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 RiutalPlease 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.

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

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("\nPlease 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("\nPlease 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("\n\nPlease enter the numbers below to perform X * Y + Z");

System.out.println("\nPlease 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("\nPlease 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

}

}

Add a comment
Know the answer?
Add Answer to:
This is java and make simple program. CPSC 1100. Thanks CSPS 1100 Lab 4 ay total...
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
  • Java Program Question about Bugs

    This program will simulate the growth and decrease of bugs based on breeding the bugs and spraying the bugs.  a. Create a Bug classb. Create an instance variable to hold the number of bugs.c. Create a constructor that sets the original number of bugs to an amount that is provided through the argument. Hint: BankAccount with an initial balance.d. Create a method call breedBugs. This method will not return anything and will not have an argument. The breedBugs method should double...

  • This is for a java program public class Calculation {    public static void main(String[] args)...

    This is for a java program public class Calculation {    public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object    //Task 1. Write your code here //Print the prompt and ask the user to enter an...

  • Last name is Vhora 3. Write a java class named FinalyourLastName, which contains three methods: main,...

    Last name is Vhora 3. Write a java class named FinalyourLastName, which contains three methods: main, arrayMystery, and countTotalOdd. You need to create main method. Inside the main method...you need to create an integer array named myld that consists of each of the digits in your student ID, call arrayMystery method and print out myld, then call count TotaOdd method and print out the total number of odd digits in your ID. (8 points) The arrayMystery method is given as...

  • in JAVA 24. Suppose that a class has an overloaded method named add with the following...

    in JAVA 24. Suppose that a class has an overloaded method named add with the following two implementations: double add (int x, double y) { return x + y; } double add (double x, int y) { return x + y + 1; } What, if anything, will be returned by the following method calls? A. add(3, 3.14) B. (3.14, 3) C. add (3, 3) D. add (3.14, 3.14) 29. Here is the code for a recursive method named mystery....

  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

    Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...

  • Adv. Java program - create a math quiz

    Create a basic math quiz program that creates a set of 10 randomly generated math questions for the following operations:AdditionSubtractionMultiplicationDivisionModuloExponentThe following source files are already complete and CANNOT be changed:MathQuiz.java (contains the main() method)OperationType.java (defines an enumeration for valid math operations and related functionality)The following source files are incomplete and need to be coded to meet the indicated requirements:MathQuestionable.javaA Random object called RANDOM has already been declared and assigned, and you must not change this assignment in any way.Declare and assign an interface integer called MAX_SMALL and assign it the...

  • Create a Java Program/Class named ComputeInterest to calculate compound interest. This can all be done entirely...

    Create a Java Program/Class named ComputeInterest to calculate compound interest. This can all be done entirely in the main() method. Create a double variable named currentBalance. Create a double variable named newBalance. Create a final double variable named INTEREST_RATE and assign 1.05 to it. Create a int variable named yearCount. Prompt for the "Number of years to invest" and store the input into the yearCount variable. Prompt for the "Amount to Invest" and store this in the currentBalance variable. Assign...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

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