Question

In: Computer Science

JAVA 1 PROGRAMMING QUESTION In this program you will be writing a class that will contain...

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

  • There are two separate files in this program. The class containing the main method is supplied, and you will write a new class containing the methods you will write.

? 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.

  • Name the file (and class) you will create to hold the methods, MethodPractice (with the addition of your name data as above). This is the file that will have all of the methods other than the main method.

The Methods to Write

  • In the file called MethodPractice you will write the following methods:
    • displayLine()

      • This method will not accept arguments nor will it return any values.
      • You may use the illustrated “Basic void Method” up above.
      • This method simply displays a line by using: System.out.println("--------------------");
      • Make sure your method displays 20 dashes.
    • displayMessage()

      • This method will accept a String as an argument, but will not return a value.
      • This method will accept a String message and simply display that exact message on the screen.
    • sumNumbers()

      • This method will accept two integers as arguments.
      • This method will add those two integers together and return the sum.
    • isGreater()

      • This method will accept two integers as arguments.
      • This method will use if and else to test the two numbers to see if the first one is greater than or equal to the second.
      • If the first number is greater than or equal to the second, the method will return a boolean true. If the first number is not greater than or equal to the second number, then the method will return a boolean false.
      • This method could use two return statements. However, use only one return statement at the end of the method. You can accomplish this by setting a variable to one boolean state of true or false, and then returning that variable.
    • setBulb()

      • This method will accept a boolean argument, but will not return a value.
      • Based on a received boolean value, this method will use if and else to display a message of either “The bulb is now on.,” or “The bulb is now off.”

The Instance Variables

? Make all instance variables in the MethodPractice class private.

  • There are instance variables at the beginning of the MethodsPractice class. You may change those, but you will need to be consistent with the method calling from the main method in the MethodTest class.

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.

  • Below is the class by which your methods will be tested. It contains the main method with calls to each of the methods you are will write in the MethodPractice class. Use this class to test your methods.
  • Remember to make the necessary modifications mentioned above.

Solutions

Expert Solution

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 *************************************************************


Related Solutions

Question: Java Programming. ** Modify Current Program. Current Program class Account{       //instance variables   ...
Question: Java Programming. ** Modify Current Program. Current Program class Account{       //instance variables    private long accountNumber;    private String firstName;    private String lastName;    private double balance;       //constructor    public Account(long accountNumber, String firstName, String lastName, double balance) {        this.accountNumber = accountNumber;        this.firstName = firstName;        this.lastName = lastName;        this.balance = balance;    }    //all getters and setters    public long getAccountNumber() {        return...
This is for Java programming. Please use ( else if,) when writing the program) Write a...
This is for Java programming. Please use ( else if,) when writing the program) Write a java program to calculate the circumference for the triangle and the square shapes: The circumference for: Triangle = Side1 + Side2 +Sid3 Square = 4 X Side When the program executes, the user is to be presented with 2 choices. Choice 1 to calculate the circumference for the triangle Choice 2 to calculate the circumference for the square Based on the user's selection the...
***In Java language***The first programming project involves writing a program that computes the salaries for a...
***In Java language***The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of four classes. 1. The first class is the Employee class, which contains the employee's name and monthly salary, which is specified in whole dollars. It should have three methods: a. A constructor that allows the name and monthly salary to be initialized. b. A method named annualSalary that returns the salary for a whole...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
CIT 149 JAVA 1 programming question. The Assignment This program will display a menu to the...
CIT 149 JAVA 1 programming question. The Assignment This program will display a menu to the user from which they can choose an option to find the area of a rectangle, the area of a triangle, or to quit. The user will make the selection and will be prompted to enter the height and width, or the base and height, depending upon which selection they made. The program will then calculate the appropriate area and display the results. This program...
write a program in java that contain a class for botique . data member include code...
write a program in java that contain a class for botique . data member include code , color , size , quantity . your class should contains all accessor and mutator methods , non paraqmetric constructor , parametric constructor , input andvidsplay method
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going to implement a simple C version of the UNIX cat program called lolcat. The cat program allows you to display the contents of one or more text files. The lolcat program will only display one file. The correct usage of your program should be to execute it on the command line with a single command line argument consisting of the name you want to...
A question about exceptions, the language is JAVA. The purpose is writing a program that reads...
A question about exceptions, the language is JAVA. The purpose is writing a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock , for example 23:47:55). Here are the input errors (Exceptions)...
java programming question: "Write a program that displays a page on a topic of interest to...
java programming question: "Write a program that displays a page on a topic of interest to you (e.g., a sport, a city). Include at least one image and one shape (which can be text). " I wanted to do a soccer page but I don't know how to put pictures from the internet on Java. As for my object, I wanted to make a soccer ball. My professor has been really confusing in explaining graphics so any help is appreciated!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT