Question

QUESTION 3 (20) Create a constructor class Sum that has public integer variables num1 and num2....

QUESTION 3 (20)
Create a constructor class Sum that has public integer variables num1 and num2. Include a default constructor and a method Add that accepts the two integer parameters, calculates and outputs the sum. In your main class Calculate include a main method that prompts the user to enter 2 integer values and create a reference called total for the constructor Sum. Output the values entered by the user and the sum of the values in the command line window.

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

Explanation::

  • Code in JAVA is given below
  • Please read comments for better understanding of the code
  • OUTPUT are given at the end of the code


Code in JAVA::

Sum.java ::

public class Sum{
   public int num1;
   public int num2;
   /**
   *Default Constructor that initializes
   */
   public Sum(){
       this.num1=0;
       this.num2=0;
   }
   public void Add(int n1,int n2){
       /**
       * Calculates the sum of n1 and n2
       * And then displays the value
       */
       int sum=n1+n2;
       System.out.println("The sum of "+n1+" and "+n2+" is "+sum);
   }
}

Calculate.java::

import java.util.*;
public class Calculate{
   public static void main(String[] args){
       /**
       * Using Scanner class object to take input
       * from the user
       */
       Scanner sc=new Scanner(System.in);
      
       /**
       * Two integer variables named n1 and n2
       * are created
       */
       int n1,n2;
      
       /**
       * Prompting user to enter two integers
       */
       System.out.print("Enter first number : ");
       n1=sc.nextInt();
      
       System.out.print("Enter second number : ");
       n2=sc.nextInt();
      
       Sum total=new Sum();
       total.Add(n1,n2);
      
   }
}

OUTPUT::

TEST CASE 1::
javac Calculate.java

java Calculate
Enter first number : 23
Enter second number : 20
The sum of 23 and 20 is 43

TEST CASE 2::
java Calculate
Enter first number : 67
Enter second number : 89
The sum of 67 and 89 is 156

Add a comment
Know the answer?
Add Answer to:
QUESTION 3 (20) Create a constructor class Sum that has public integer variables num1 and num2....
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
  • Create a class calc with two data members (num1 & num2). The class should have a...

    Create a class calc with two data members (num1 & num2). The class should have a constructor that receives arguments during object creation and sets the values of the members. The class should also have a function called divide() that divides the two numbers and returns the result. The function should throw an exception if the denominator is 0. Create two objects of the class in the main() function. Use class templates making the first object an int and the...

  • Exercise #3: Create the “MathTest” class. It will have two class variables: 1) a question and...

    Exercise #3: Create the “MathTest” class. It will have two class variables: 1) a question and 2) the answer to that question. Exercise #4: Please create an accessor and mutator method for both of those variables, without which we would not be able to see or change the question or the answer. Exercise #5: There should be a constructor method. We will also have a “ToString” method, which will print the question followed by its answer. The constructor method has...

  • What I need: Create a new program named Reverse4 and declare four integer variables with the...

    What I need: Create a new program named Reverse4 and declare four integer variables with the values 23, 45, 55, and 67. Add a method named Reverse that reverses the positions of four integer variables. The Reverse method should accept the variables as references. Write a Main() method that displays the four variables both before and after reversing them to ensure the method works correctly. What I have: using System; class Reverse4 { public static void reverse(ref int num1, ref...

  • Create a CalculatorTest class that contains the main method. 1. Get two double numbers as input...

    Create a CalculatorTest class that contains the main method. 1. Get two double numbers as input and create an object of the ScientificCalculator Class with those two numbers. 2. Then call the 8 operations one by one sequentially and display the result. Code must be written in Java. (Sample input/output: Enter number 1: 2 Enter number 2: 3 Add() result is : 5 Sub() result is: -1 ……. Power() result is: 8 …… sqrtNum1 result is:) The Problem: Given the...

  • 1- Create the base class Book that has the following instance variables, constructor, and methods title...

    1- Create the base class Book that has the following instance variables, constructor, and methods title ( String) isbn ( String) authors (String) publisher (String) edition ( int) published_year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method // that return sting representation of Book object. 2- Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title ( String) isbn...

  • Create 3 user-defined methods called add(). 1 accepts a single integer array parameter; this method should...

    Create 3 user-defined methods called add(). 1 accepts a single integer array parameter; this method should loop the array adding the values and return the summation (use 4, 1, 4, 6, 10, 15, 32, 79, 18 as the values in the array) 1 accepts 2 integer parameters; this method should add them and return the value 1 accepts 3 integer parameters; this method should add them all and return the value In main, define the parameters above (you may use...

  • Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and re...

    Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and return their total. The main method then prints the digits total with proper label as shown below Method Sum )is of type integer and takes an integer value. The method recursively adds up the digits and returns their total. Document your code and use proper prompts for...

  • Create a LIFO class with following methods in java - public LifoList() The default constructor for...

    Create a LIFO class with following methods in java - public LifoList() The default constructor for LifoList class which will initialize your class variables maxSize to 0 and the int array to null. public LifoList(int maxSize) The constructor for LifoList class which takes the int input maxSize to initialize the size of the array. You should NOT reinitialize the array elsewhere. For example, creating an object as new LifoList(5) should create a LifoList whose maximum size is 5. If the...

  • Programming: Create a class called City with two public variables: a string to store the name...

    Programming: Create a class called City with two public variables: a string to store the name of the city and a float to store the average temperature in Fahrenheit. Create one object of the class that you create and ask the user to enter the city name and the average temperature in Fahrenheit. Store these in the object of the class that you create. Then display the contents of the class. Once that is working, make the variables private and...

  • PYTHON Task 1 Create a class called Window It has 2 public properties 1 private property...

    PYTHON Task 1 Create a class called Window It has 2 public properties 1 private property 1 constructor that takes 3 arguments pass each argument to the appropriate property Task 2 In a new file import the Window class Instantiate the Window object Output the two public properties Task 3 Alter the Window class in Task 1 Add an accessor and mutator (the ability to access and change) to the private property Task 4 Create a class named Instrument Give...

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