This is java and make simple program. CPSC 1100. Thanks CSPS 1100 Lab 4 ay total
ID: 3756822 • Letter: T
Question
This is java and make simple program. CPSC 1100. Thanks
CSPS 1100 Lab 4 ay total Also I would need a new name for the daubles. An example here might be double tRtalD-caradd xD, YD) 1. (a) We are going to begin with some simple math, reading input, and formatted output. Create a class called MuCalsustec Yau will not have an instance variable. Since you have no instance variables to initialize, you do nat need a constructar. Do NOT create a constructor Create a method named add. This method should have two arguments, X and Y (Both of these should be integers The method should add X and Y together and return the result. Remember the return type needs to be an int. Create a method named add. This method should have two arguments, X and Y (Both of these should be floating paint. The methad should add X and Y together and return the result. Remember the return type need to be a dauble. This is an example of an overload for a method. The campiler will chaose which method to use based on the values being à. b. c. 1.(bl We are going to redo aur calculator a little to allow it to calculate more than two numbers q. Copy both your MuCawstec and oekustaTeste Change the names to d. . Add an instance variable called total This will hold yaur integer totals. t. Add an instance variable called tatah. This will hald your double totals. u. Add a constructor with no parameters that sets the value of total and totalto a. v. Change the return type of each af your methods to vaid. w. Remave the return statement from each of your methods. x Use the tatal to hold the values. For erample totalX+Y y. Create a methad called getTotel) similar to get BalanceRemember this will return an e. f. Repeat steps d and e for subtract, divide, and multiply. 8. Create auatarTester class. Remember this class has the main and no methads. integer 2. Create a methad called geotl This will return the double. aa. Create a methad called deaThis method will set total toQ. Do nat return anything. bb. Create a methad called deaD This method will set totallto Q. Do nat return i. Create an object af type Scanner. I suggest yaur use the name in ar input so yau will know . Write the code to prompt the user to input the value for X. Use something like this. k. Create a variable of type int named X to receive the input. There is an example in yaur book what you are doing. utam.aut Riutal"Please enter the value for X as an integer: "I on page 145 in the box named Syntax 4.3. anything dd. Every time it says Mutalcuator change to betelsustocadaRRR ee. Add anather prompt. Ask the use to enter another integer number. Yau will need anather L Write the code to prompt the user to input the value for Y using step j as an example. m. Repeat step k except you want to name the variables Y n. Repeat steps j- asking far doubles rather than integers. Yau will need to change the integer variable for this ff. Add anather prompt to ask the user to enter anather double number. Yau will need another double variable for this. names of each af the variables. Perhaps XD and YD would work. (After this step, you should have 4 variables, one for a X integer value, ane for a Y integer value, one for a X double value, and one for a Y dauble value. Create an object af type MCaculator This would loak very much like the abject you created forBaakawu 88. Calculate X +Y-Z. If you did nat create a variable named total in the previous prablem, do so now. Yau will need a total hh. Call add using x and v; i. Call gettotal jj. Call subtract using the total fram steps. Gakwbtwttataw; kk. Call gettotall again to get the new result IL. Print the result. Be sure and print the formula with the result mm. Call the deartal Remember to use the object name. nn. If you did not create a variable named total in the previaus prablem, do so naw. You will a. using the default constructor. Like this: Using the object your created call each of the methods and print the results. You have 2 options at this point yau may create new variables to hold the results such as int total- calcddY and then print total. Yau may also print straight from the call like p. e sum of X and Y is:fakaddwù ); A couple on notes at this point. The word cale is the name of the abject. Yaur name may be different. Where l put int total, the int would only apply to the integer values and you can anly declare a type once. What that means is that if I were creating a total and using it for integer add, subtract, divide, and multiple, I would anly put int tatal ane time. After that I would simply need a tatah ao. Repeat stepsq-v far doubles instead af integers. pp. Repeat steps ry far the following formula. X·Y-Z for both integers and doubles.Explanation / Answer
1 a) Answer
import java.util.Scanner;
// Class MyCalculator definition
class MyCalculator
{
// Method to add two integer parameter x and y value and returns the sum
int add(int x, int y)
{
return (x + y);
}// End of method
// Overloaded method to add two double parameter x and y value and returns the sum
double add(double x, double y)
{
return (x + y);
}// End of method
// Method to subtract two integer parameter x and y value and returns the result
int subtract(int x, int y)
{
return (x - y);
}// End of method
// Overloaded subtract to add two double parameter x and y value and returns the sum
double subtract(double x, double y)
{
return (x - y);
}// End of method
// Method to multiply two integer parameter x and y value and returns the result
int miltiply(int x, int y)
{
return (x * y);
}// End of method
// Overloaded multiply to add two double parameter x and y value and returns the sum
double miltiply(double x, double y)
{
return (x * y);
}// End of method
// Method to divide two integer parameter x and y value and returns the result
int divide(int x, int y)
{
return (x / y);
}// End of method
// Overloaded method to divide two double parameter x and y value and returns the sum
double divide(double x, double y)
{
return (x / y);
}// End of method
}// End of class MyCalculator
// Defines driver class MyCalculatorTester
public class MyCalculatorTester
{
// Defines main method
public static void main(String[] args)
{
// Scanner class object created
Scanner sc = new Scanner(System.in);
// Creates an object of the class MyCalculator using default constructor
MyCalculator mc = new MyCalculator();
// Declares local integer variable to store data
int xI, yI;
// Declares local double variable to store data
double xD, yD;
// Accepts two integer data
System.out.print(" Please enter the value of X as integer: ");
xI= sc.nextInt();
System.out.print(" Please enter the value of Y as integer: ");
yI= sc.nextInt();
// Calls the method to perform integer addition and displays the result
System.out.println("Integer: " + xI + " + " + yI + " = " + mc.add(xI, yI));
// Calls the method to perform integer subtraction and displays the result
System.out.println("Integer: " + xI + " - " + yI + " = " + mc.subtract(xI, yI));
// Calls the method to perform integer multiplication and displays the result
System.out.println("Integer: " + xI + " * " + yI + " = " + mc.miltiply(xI, yI));
// Calls the method to perform integer division and displays the result
System.out.println("Integer: " + xI + " / " + yI + " = " + mc.divide(xI, yI));
// Accepts two double data
System.out.print(" Please enter the value of X as double: ");
xD= sc.nextDouble();
System.out.print(" Please enter the value of Y as double: ");
yD= sc.nextDouble();
// Calls the method to perform double addition and displays the result
System.out.println("Double: " + xD + " + " + yD + " = " + mc.add(xD, yD));
// Calls the method to perform double subtraction and displays the result
System.out.println("Double: " + xD + " - " + yD + " = " + mc.subtract(xD, yD));
// Calls the method to perform double multiplication and displays the result
System.out.println("Double: " + xD + " * " + yD + " = " + mc.miltiply(xD, yD));
// Calls the method to perform double division and displays the result
System.out.println("Double: " + xD + " / " + yD + " = " + mc.divide(xD, yD));
}// End of main method
}// End of driver class MyCalculatorTester
Sample Output:
Please enter the value of X as integer: 42
Please enter the value of Y as integer: 7
Integer: 42 + 7 = 49
Integer: 42 - 7 = 35
Integer: 42 * 7 = 294
Integer: 42 / 7 = 6
Please enter the value of X as double: 12.5
Please enter the value of Y as double: 2.5
Double: 12.5 + 2.5 = 15.0
Double: 12.5 - 2.5 = 10.0
Double: 12.5 * 2.5 = 31.25
Double: 12.5 / 2.5 = 5.0
-----------------------------------------------------------------------------------------
1 b) Answer
import java.util.Scanner;
// Class MyAdcanceCalculator definition
class MyAdcanceCalculator
{
// Declares an instance variable to store integer calculates result
int total;
// Declares an instance variable to store double calculates result
double totalD;
// Default constructor to initialize instance variables
MyAdcanceCalculator()
{
total = 0;
totalD = 0.0;
}// End of default constructor
// Method to add two integer parameter x and y value and stores the result in total
void add(int x, int y)
{
total = (x + y);
}// End of method
// Overloaded method to add two double parameter x and y value and stores the result in totalD
void add(double x, double y)
{
totalD =(x + y);
}// End of method
// Method to subtract two integer parameter x and y value and stores the result in total
void subtract(int x, int y)
{
total = (x - y);
}// End of method
// Overloaded subtract to add two double parameter x and y value and stores the result in totalD
void subtract(double x, double y)
{
totalD =(x - y);
}// End of method
// Method to multiply two integer parameter x and y value and stores the result in total
void miltiply(int x, int y)
{
total = (x * y);
}// End of method
// Overloaded multiply to add two double parameter x and y value and stores the result in totalD
void miltiply(double x, double y)
{
totalD =(x * y);
}// End of method
// Method to divide two integer parameter x and y value and stores the result in total
void divide(int x, int y)
{
total = (x / y);
}// End of method
// Overloaded method to divide two double parameter x and y value and stores the result in totalD
void divide(double x, double y)
{
totalD = (x / y);
}// End of method
// Method to return integer total
int getTotal()
{
return total;
}// End of method
// Method to reset the integer total to 0
void clearTotal()
{
total = 0;
}// End of method
// Method to return double total
double getTotalD()
{
return totalD;
}// End of method
// Method to reset the double total to 0
void clearTotalD()
{
totalD = 0.0;
}// End of method
}// End of class MyAdcanceCalculator
// Defines driver class MyAdcanceCalculatorTester
public class MyCalculatorAdvanceTester
{
// Defines main method
public static void main(String[] args)
{
// Scanner class object created
Scanner sc = new Scanner(System.in);
// Creates an object of the class MyAdcanceCalculator using default constructor
MyAdcanceCalculator mc = new MyAdcanceCalculator();
// Declares local integer variable to store data
int xI, yI, zI, total, result;
// Declares local double variable to store data
double xD, yD, zD, totalD, resultD;
// Accepts three integer data
System.out.print(" Please enter the value of X as integer: ");
xI = sc.nextInt();
System.out.print(" Please enter the value of Y as integer: ");
yI = sc.nextInt();
System.out.print(" Please enter the value of Z as integer: ");
zI = sc.nextInt();
// Calls the method to calculate integer expression (X + Y - Z)and displays the result
// Calls the method to add xI and yI
mc.add(xI, yI);
// Calls the method to get the total and stores the return result
total = mc.getTotal();
// Calls the method to subtract zI from previous total
mc.subtract(total, zI);
// Calls the method to get the total and stores the return result
result = mc.getTotal();
mc.clearTotal();
// Displays the expression result
System.out.println(" Integer Expression: " + xI + " + " + yI + " - " + zI + " = " + result);
// Accepts two double data
System.out.print(" Please enter the value of X as double: ");
xD = sc.nextDouble();
System.out.print(" Please enter the value of Y as double: ");
yD = sc.nextDouble();
System.out.print(" Please enter the value of Z as double: ");
zD = sc.nextDouble();
// Calls the method to calculate double expression (X * Y / Z)and displays the result
// Calls the method to multiply xI and yI
mc.miltiply(xD, yD);
// Calls the method to get the total and stores the return result
totalD = mc.getTotalD();
// Calls the method to divide previous product with zD
mc.divide(totalD, zD);
// Calls the method to get the total and stores the return result
resultD = mc.getTotalD();
// Displays the expression result
System.out.println(" Double Expression: " + xD + " x " + yD + " / " + zD + " = " + resultD);
}// End of main method
}// End of driver class MyCalculatorTester
Sample Output:
Please enter the value of X as integer: 12
Please enter the value of Y as integer: 5
Please enter the value of Z as integer: 6
Integer Expression: 12 + 5 - 6 = 11
Please enter the value of X as double: 12.2
Please enter the value of Y as double: 2.1
Please enter the value of Z as double: 2.4
Double Expression: 12.2 x 2.1 / 2.4 = 10.675
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.