Lesson 4 - Sending Info to Class with a Constructor
import java.io.*;
public class decision2
{
public int a;
public decision2(int n)
{ a = n;
}
public void start() throws IOException
{input in = new input();
if (a % 2 == 0)
System.out.println("It is even");
else
System.out.println("It is odd");
}
}
- Copy and paste the above class into BlueJ. Call project decision. New Class - decision2
- Compile it.
- Create an instance of it by right mouse clicking on it.
- New - decision2()
- Enter a number to give n a value.
- Right Mouse click on instance of class that has been created (in Red).
- Click on start() function.
|