Question

Write a Java class (name it TwoNumbers) that will manage two numbers. The class should have...

Write a Java class (name it TwoNumbers) that will manage two numbers.

The class should have two integer variables.

The class should have the following constructors:

- TwoNumbers();

- TwoNumbers(3, 4);

The first constructor should initialize the class variables to zero.

The second constructor should initialize the class variables to the passed variables.

The class should have the following methods:

- setNumbers(3, 4);

- setFirstNum(3);

- setSecondNum(4);

- getSum();

- getDiff();

- getProd();

The setNumbers(3, 4) method should initialize the two variables of the class to the passed values.

The setFirstNum(3) method should initialize the first variable of the class to the passed value.

The setSecondNum(4) method should initialize the second variable of the class to the passed value.

The getSum() method should return the sum of the two variables in the class.

The getDiff() method should return the difference of the two variables in the class.

The getProd() method should return the product of the two variables in the class.

Also add the method main to call on the methods above.

Please write out the code. No screen shots please.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Code:

public class TwoNumbers{

   private int x,y;

   //Constructors
  
   public TwoNumbers()
   {
       x = 0;
       y = 0;
   }
  
   public TwoNumbers(int a,int b)
   {
       x = a;
       y = b;
   }

    //This will change both x and y to given arguments
   public void setNumbers(int a,int b)
   {
       x = a;
       y = b;
   }

    //This will change x to given argument
   public void setFirstNum(int a)
   {
       x = a;
   }
  
    //This will change y to given argument
   public void setSecondNum(int b)
   {
       y = b;
   }

    //returns the sum
   public int getSum()
   {
       return x+y;
   }

    //return the difference
   public int getDiff()
   {
       return x-y;
   }


    //return the product
   public int getProd()
   {
       return x*y;
   }
  
    public static void main(String []args){

       //Creating an object for the class
      
        TwoNumbers twonum = new TwoNumbers(3,4);
        System.out.println("The numbers are 3 and 4");
        System.out.println("Sum is "+twonum.getSum());
        System.out.println("Difference is "+twonum.getDiff());
        System.out.println("Product is "+twonum.getProd());

        twonum.setNumbers(5,4);
        System.out.println("The numbers are changed to 5 and 4");
        System.out.println("Sum is "+twonum.getSum());
        System.out.println("Difference is "+twonum.getDiff());
        System.out.println("Product is "+twonum.getProd());

     }
}

---------------------------------------------------------------------------------------------------------

Feel free to comment in case you have any doubts.Please don't forget to rate the question.

Add a comment
Know the answer?
Add Answer to:
Write a Java class (name it TwoNumbers) that will manage two numbers. The class should have...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a JAVA program that declares a class Pet  With three variables Name, Age, weight .The class...

    Write a JAVA program that declares a class Pet  With three variables Name, Age, weight .The class has  different constructors the first without parameters ,the  second with three parameters and the third with two parameters. In the main method  create three objects using thee different constructors to initialize the Variables.  then print out all the information a bout all objects .

  • Anyone helps me in a Java Languageif it it is possible thanks. Write a class called...

    Anyone helps me in a Java Languageif it it is possible thanks. Write a class called Player that holds the following information: Team Name (e.g., Ravens) . Player Name (e.g., Flacco) . Position's Name (e.g. Wide reciver) . Playing hours per week (e.g. 30 hours per week). Payment Rate (e.g., 46 per hour) . Number of Players in the Team (e.g. 80 players) . This information represents the class member variables. Declare all variables of Payer class as private except...

  • In java code: Write a Temperature class that has two private instance variables: • degrees: a...

    In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...

  • Java Write a Temperature class that has two private instance variables: • degrees: a double that...

    Java Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have - four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number...

    Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include...

  • JAVA QUESTION 16 Write and submit the java source for the following class specification: The name...

    JAVA QUESTION 16 Write and submit the java source for the following class specification: The name of the class is Question_16 Class Question_16 has 3 instance variables: name of type String, age of type int and height of type double. Class Question_16 has the following methods: print - outputs the data values stored in the instance variables with the appropriate label setName - method to set the name setAge - method to set the age setHeight - method to set...

  • In JAVA Algorithm Workbench 1. Write the first line of the definition for a Poodle class....

    In JAVA Algorithm Workbench 1. Write the first line of the definition for a Poodle class. The class should extend the Dog class. 2. Look at the following code, which is the first line of a class definition: public class Tiger extends Felis In what order will the class constructors execute? 3. Write the statement that calls a superclass constructor and passes the arguments x, y, and z. 4. A superclass has the following method: public void setValue(int v) value...

  • 1. Assume you have a Car class that declares two private instance variables, make and model....

    1. Assume you have a Car class that declares two private instance variables, make and model. Write Java code that implements a two-parameter constructor that instantiates a Car object and initializes both of its instance variables. 2. Logically, the make and model attributes of each Car object should not change in the life of that object. a. Write Java code that declares constant make and model attributes that cannot be changed after they are initialized by a constructor. Configure your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT