Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is java and make simple program. CPSC 1100. Thanks CSPS 1100 Lab 4 ay total

ID: 3756821 • 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

Hi, I have tried to answer in the most simplest and understandable form. All comments are explanatory of the functions performed by methods and the method calls. Please revert in case there is any confusion. Just create a new java class with name MyCalculator and paste the answer given for a below. Make another class with name MyCalculatorAdvanced and paste the answer given for b below.

The answer for part a is :

import java.util.Scanner;

public class MyCalculator {

public int add(int x, int y) { // adding function for integer inputs

return (x+y);

}

public double add(double x, double y) { // adding function for double inputs

return (x+y);

}

public int subtract(int x, int y) { // subtracting function for integer inputs

return (x-y);

}

public double subtract(double x, double y) { // subtracting function for double inputs

return (x-y);

}

public int multiply(int x, int y) { // multiplication function for integer inputs

return (x*y);

}

public double multiply(double x, double y) { // multiplication function for double inputs

return (x*y);

}

public int divide(int x, int y) { // division function for integer inputs

return (x/y);

}

public double divide(double x, double y) { // division function for double inputs

return (x/y);

}

public static void main(String[] args) {

}

}

class MyCalculatorTester {

public static void main(String[] args) {

MyCalculator myCalc = new MyCalculator(); // object of MyCalculator class

System.out.println("Please enter the numbers below to perform mathematical operations!");

Scanner input = new Scanner(System.in);

System.out.println("Please enter the value for x as an integer: ");

int x = input.nextInt();

System.out.println("Please enter the value for y as an integer: ");

int y = input.nextInt();

System.out.println("The sum of x and y is : "+myCalc.add(x, y));

System.out.println("The difference of x and y is : "+myCalc.subtract(x, y));

System.out.println("The product of x and y is : "+myCalc.multiply(x, y));

System.out.println("The quotient of x and y is : "+myCalc.divide(x, y));

System.out.println("Please enter the value for x as a double: ");

double xd = input.nextDouble();

System.out.println("Please enter the value for y as a double: ");

double yd = input.nextDouble();

System.out.println("The sum of x and y is : "+myCalc.add(xd, yd));

System.out.println("The difference of x and y is : "+myCalc.subtract(xd, yd));

System.out.println("The product of x and y is : "+myCalc.multiply(xd, yd));

System.out.println("The quotient of x and y is : "+myCalc.divide(xd, yd));

}

}

The answer for part b is :

import java.util.Scanner;

public class MyCalculatorAdvanced {

int total;

double totalD;

public MyCalculatorAdvanced() { // default constructor to initialize the instance variables

total = 0; // variable for integer totals

totalD = 0; // variable for double totals

}

public int getTotal() { // method to read the value of integer total

return total;

}

public double getTotalD() { // method to read the value of double total

return totalD;

}

public void clearTotal() { // method to reset the value of integer total to 0

total = 0;

}

public void clearTotalD() { // method to reset the value of double total to 0

totalD = 0;

}

public void add(int x, int y) { // adding function for integer inputs

total = (x+y);

}

public void add(double x, double y) { // adding function for double inputs

totalD = (x+y);

}

public void subtract(int x, int y) { // subtracting function for integer inputs

total = (x-y);

}

public void subtract(double x, double y) { // subtracting function for double inputs

totalD = (x-y);

}

public void multiply(int x, int y) { // multiplication function for integer inputs

total = (x*y);

}

public void multiply(double x, double y) { // multiplication function for double inputs

totalD = (x*y);

}

public void divide(int x, int y) { // division function for integer inputs

total = (x/y);

}

public void divide(double x, double y) { // division function for double inputs

totalD = (x/y);

}

public static void main(String[] args) {

}

}

class MyCalculatorAdvancedTester {

public static void main(String[] args) {

MyCalculatorAdvanced myCalc = new MyCalculatorAdvanced(); // object of MyCalculator class

// X + Y - Z

System.out.println("Please enter the numbers below to perform X + Y - Z");

Scanner input = new Scanner(System.in);

System.out.println(" Please enter the value for x as an integer: ");

int x = input.nextInt();

System.out.println("Please enter the value for y as an integer: ");

int y = input.nextInt();

System.out.println("Please enter the value for z as an integer: ");

int z = input.nextInt();

myCalc.add(x, y); //x+y

int total = myCalc.getTotal();

myCalc.subtract(total, z); //x+y-z

total = myCalc.getTotal();

System.out.println("The result of (x + y - z) is : "+total);

myCalc.clearTotal(); // reset the total value to 0

System.out.println(" Please enter the value for x as a double: ");

double xd = input.nextDouble();

System.out.println("Please enter the value for y as a double: ");

double yd = input.nextDouble();

System.out.println("Please enter the value for z as a double: ");

double zd = input.nextDouble();

myCalc.add(xd, yd); //xd+yd

double totalD = myCalc.getTotalD();

myCalc.subtract(totalD, zd); //xd+yd-zd

totalD = myCalc.getTotalD();

System.out.println("The result of (x + y - z) is : "+totalD);

myCalc.clearTotalD(); // reset the totalD value to 0

// X * Y + Z

System.out.println(" Please enter the numbers below to perform X * Y + Z");

System.out.println(" Please enter the value for x as an integer: ");

x = input.nextInt();

System.out.println("Please enter the value for y as an integer: ");

y = input.nextInt();

System.out.println("Please enter the value for z as an integer: ");

z = input.nextInt();

myCalc.multiply(x, y); //x*y

total = myCalc.getTotal();

myCalc.add(total, z); //x*y+z

total = myCalc.getTotal();

System.out.println("The result of (x * y + z) is : "+total);

myCalc.clearTotal(); // reset the total value to 0

System.out.println(" Please enter the value for x as a double: ");

xd = input.nextDouble();

System.out.println("Please enter the value for y as a double: ");

yd = input.nextDouble();

System.out.println("Please enter the value for z as a double: ");

zd = input.nextDouble();

myCalc.multiply(xd, yd); //xd*yd

totalD = myCalc.getTotalD();

myCalc.add(totalD, zd); //xd*yd+zd

totalD = myCalc.getTotalD();

System.out.println("The result of (x + y - z) is : "+totalD);

myCalc.clearTotalD(); // reset the totalD value to 0

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote