In: Computer Science
JAVA 1 PROGRAMMING QUESTION
In this program you will be writing a class that will contain
some methods. These will be regular methods (not static methods),
so the class will have to be instantiated
in order to use them.
The class containing the main method will
be provided and you will write the required methods and run the
supplied class in order to test those methods.
? Specifications
? The code for the testing class is given below. In order to test the code you write, you will need to rename this file as you have all of your program files, as outlined above. You will need to change the calls to the methods to reflect the class where your methods are located. Do not modify the logic in this file to make it fit your methods. Part of this problem is to meet the specifications of the test program.
The Methods to Write
displayLine()
displayMessage()
sumNumbers()
isGreater()
setBulb()
The Instance Variables
? Make all instance variables in the MethodPractice class private.
The MethodTest.java Class
? Be sure to rename this file as you do all of your files, as outlined above, and upload this file along with the MethodPractice file. You will also need to modify the method calls to match your class name for the MethodPractice class.
MethodPractice.java
public class MethodPractice {
public void displayLine()
{
System.out.println("--------------------");
}
public void displayMessage(String message)
{
System.out.println(message);
}
public int sumNumbers(int first, int second)
{
return (first + second);
}
public boolean isGreater(int first, int second)
{
boolean result;
if(first >= second)
result = true;
else
result = false;
return result;
}
public void setBulb(boolean val)
{
if(val)
displayMessage("The bulb is now on.");
else
displayMessage("The bulb is now off.");
}
}
MethodTest.java (Main class)
public class MethodTest {
public static void main(String[] args)
{
MethodPractice practice = new MethodPractice();
practice.displayLine();
practice.displayMessage("WELCOME TO METHOD PRACTICE TEST\n");
int sum = practice.sumNumbers(67, 99);
practice.displayMessage("The sum of 67 and 99 is = " + sum);
boolean greater = practice.isGreater(67, 99);
practice.setBulb(greater);
}
}
Note: As there are no instance variables mentioned in the question, it couldn't be answered. In case there are any, please add "private" before the variable declarations. For eg.: private String name; OR private int number;
************************************************************** SCREENSHOT *************************************************************