Question

In: Computer Science

The following program is written to calculate the addition for two numbers (9,3).  Unfortunately, the program has...

The following program is written to calculate the addition for two numbers (9,3).  Unfortunately, the program has Compile-time and Run-time errors that prevent the program from running and producing the correct result. Using the Table 2.1 below, allocate the error(s) on each program line.

  1. // define new class
  2. publicCalculation {
  1. privateintresult_add
  2. // define add method to add two numbers
  3. publicvoidadd(intfirst_number,intsecond_number) {
  4. int   number1= first_number;
  5. doublenumber2= second_number;
  6. result_add= number1/number2;
  1. }
  2. / return the value
  3. publicintgetvalue()
  4. {
  5. returnresult_add;
  6. }
  7. publicstaticvoidmain(String[] args) {
  8. // define new object with name summation
  1. Calculation summation= Calculation();
  2. summation.add(9,3);

  1. intresult= getvalue();
  2. System.println(result);

  1. }
  1. }

  1. }

Solutions

Expert Solution

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Below are the errors:

  • Clas keyword missing at the first line that is public class Calculation
  • In method as two parameters of integer type are passed and in the definition for second_number double type is used hence on the line result_add= number1/number2; getting error hence need to change this.
  • Alson on the same line needs to used + operator instead of /
  • On the line Calculation summation= Calculation(); fore creating object new keyword is missing
  • getvalue() needs to be called using object name that is summation
  • System.println(result); line missing keyword out
  • At the last line } is needs to be removed.

Demonstration :

Here a new java program with name "Calculation.java" is created, which contains following code.
Calculation.java :

// define new class
public class Calculation {
   private int result_add;

// define add method to add two numbers
   public void add(int first_number, int second_number) {
       int number1 = first_number;
       int number2 = second_number;
       result_add = number1+ number2;
   }

//return the value
   public int getvalue() {
       return result_add;
   }

//entry point of program ,main method
   public static void main(String[] args) {
// define new object with name summation
       Calculation summation = new Calculation();
       summation.add(9, 3);
       int result = summation.getvalue();
       System.out.println(result);
   }
}

======================================================

Output : Compile and Run Calculation.java to get the screen as shown below

Screen 1 :Calculation.java

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers....
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers. Start your code by copying/pasting this information into an editor like notepad, notepad++, or IDLE: public class Main { public static void main(String[] args) { // Write your code here } } Sample input: Input first number: 125 Input second number: 24 Sample Output: 125 + 24 = 149 125 - 24 = 101 125 x 24 = 3000 125 / 24 = 5...
Write a JavaFx program for addition of two numbers. (create text as Number1, Number2 and Result...
Write a JavaFx program for addition of two numbers. (create text as Number1, Number2 and Result and create 3 textfileds. Create sum button. You have to enter number1 and number2 in the textfileds. If you click the button Sum, the result will be showed in the third text field.
This project involves writing a program to calculate the terms of the following sequence of numbers:...
This project involves writing a program to calculate the terms of the following sequence of numbers: 0 1 1 3 5 11 21 43 … where each term is twice the second previous term plus the previous term. The 0th term of the sequence is 0 and the 1st term of the sequence is 1. The example below shows how to calculate the next sequence term: Current sequence: 0 1 Calculate next term: 2 * 0 + 1 = 1...
Write a program in python language, which accepts 2 numbers and a + sign (for addition)...
Write a program in python language, which accepts 2 numbers and a + sign (for addition) A sign - (for subtraction) A sign * (for multiplication), / (for division) Then calculate and to display the result of the operation he chose with the two numbers. Displaying the appropriate message
########### ##4. The following code contains several nested if-else statements. Unfortunately, it ##was written without proper...
########### ##4. The following code contains several nested if-else statements. Unfortunately, it ##was written without proper alignment and indentation. Rewrite the code and use the ##proper conventions of alignment and indentation. ## Start with input('Enter integer score: ') for the variable score. ##A_score = 90 ##B_score = 80 ##C_score = 70 ##D_score = 60 ##if score >= A_score: ##print('Your grade is A.') ##else: ##if score >= B_score: ##print('Your grade is B.') ##else: ##if score >= C_score: ##print('Your grade is C.')...
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199 Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199   Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
Design the logic for a program that has two parts: The user enters 15 numbers that...
Design the logic for a program that has two parts: The user enters 15 numbers that are stored into an array. The program uses the array to display the numbers back to the user in reverse order. You must use an array and one or more loops to get full credit. Can someone please help me figure this out?
Write a C++ program to generate two random numbers (Rnd1 and Rnd2). These two numbers should...
Write a C++ program to generate two random numbers (Rnd1 and Rnd2). These two numbers should be within a range [100, 500]. If Rnd1 greater than or equals to Rnd2, print out the result of (Rnd1-Rnd2). Otherwise, print out the result of (Rnd2-Rnd1). In all cases, print Rnd1, Rnd2, and the results of subtractions in suitable messages.
This program will read a bunch of numbers from an external file (numbers.txt) and calculate their...
This program will read a bunch of numbers from an external file (numbers.txt) and calculate their total and average. In addition, the total will be multiplied by an integer that is supplied by the user. ** IMPORTANT: The numbers are included in your starter code as a separate file (numbers.txt). Don't touch that file! All of your code should be placed in code.cpp, as usual. ** Input: After all numbers are read, what number would you like to multiply the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT