This is java and make simple program. CPSC 1100. Thanks
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
}
}
This is java and make simple program. CPSC 1100. Thanks CSPS 1100 Lab 4 ay total...
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) { 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, 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 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 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 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 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...
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 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) 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...