Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

must be in java 1. We are going to begin with some simple math, reading input, and formatted output. Create a class called MyCalculator. You

must be in java

1. We are going to begin with some simple math, reading input, and formatted output.

Create a class called MyCalculator.

You will not have an instance variable.

Since you have no instance variables to initialize, you do not need a constructor. 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 point. The method should add X and Y together and return the result. Remember the return type need to be a double. This is an example of an overload for a method. The compiler will choose which method to use based on the values being sent.

Repeat steps d and e for subtract, divide, and multiply.

Create a MyCalculatorTester class. Remember this class has the main and no methods.

Import java.util.Scanner;

Create an object of type Scanner. I suggest your use the name in or input so you will know what you are doing.

Write the code to prompt the user to input the value for X. Use something like this.

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

Create a variable of type int named X to receive the input. There is an example in your book on page 145 in the box named Syntax 4.3.

Write the code to prompt the user to input the value for Y using step j as an example.

Repeat step k except you want to name the variables Y.

Repeat steps j m asking for doubles rather than integers. You will need to change the names of each of the variables. Perhaps XD and YD would work. (After this step, you should have 4 variables, one for a X integer value, one for a Y integer value, one for a X double value, and one for a Y double value.

Create an object of type MyCalculator. This would look very much like the object you created for BankAccount using the default constructor. Like this:

BankAccount harryAccount = new BankAccount();

Using the object your created call each of the methods and print the results. You have 2 options at this point you may create new variables to hold the results such as int total = calc.add(X,Y) and then print total. You may also print straight from the call like System.out.println(The sum of X and Y is: + calc.add(x,y) ); A couple on notes at this point. The word calc is the name of the object. Your name may be different. Where I put int total, the int would only apply to the integer values and you can only 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 only put int total one time. After that I would simply say total = . Also I would need a new name for the doubles. An example here might be double totalD = calc.add(XD, YD);)

2. We are going to redo our calculator a little to allow it to calculate more than two numbers.

Copy both your MyCalculator and MyCalculatorTester. Change the names to MyCalculatorAdvanced and MyCalculatorAdvancedTester.

Open the MyCalculatorAdvanced.

Add an instance variable called total. This will hold your integer totals.

Add an instance variable called totalD. This will hold your double totals.

Add a constructor with no parameters that sets the value of total and totalD to 0.

Change the return type of each of your methods to void.

Remove the return statement from each of your methods.

Use the total to hold the values. For example total = X + Y.

Create a method called getTotal() similar to getBalance(). Remember this will return an integer.

Create a method called getTotalD(). This will return the double.

Create a method called clearTotal(). This method will set total to 0. Do not return anything.

Create a method called clearTotalD(). This method will set totalD to 0. Do not return anything.

Open your MyCalculatorAdvancedTester.

Every time it says MyCalculator change to MyCalculatorAdvanced.

Add another prompt. Ask the use to enter another integer number. You will need another integer variable for this.

Add another prompt to ask the user to enter another double number. You will need another double variable for this.

Calculate X + Y Z. If you did not create a variable named total in the previous problem, do so now. You will need a total.

Call add using x and y;

Call getTotal();

Call subtract using the total from step s. calc.subtract(total,z);

Call getTotal() again to get the new result.

Print the result. Be sure and print the formula with the result.

Call the clearTotal(). Remember to use the object name.

If you did not create a variable named total in the previous problem, do so now. You will need a totalD.

Repeat steps q-v for doubles instead of integers.

Repeat steps r-y for the following formula. X * Y + Z for both integers and doubles.

3. I hope this program is a little fun but it will cause you to think. This program will simulate the growth and decrease of bugs based on breeding the bugs and spraying the bugs. This is a program where you will work most on your own without a lot of guidance from me. As always if you work on this early, I will answer questions.

Create a Bug class

Create an instance variable to hold the number of bugs.

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.

Create a method call breedBugs. This method will not return anything and will not have an argument. The breedBugs method should double the number of bugs each time it is invoked or called

Create a method called sprayBugs. This method will not return anything and will not have an argument. The sprayBugs should decrease the number of bugs by 1/4 each time it is invoked. Multiply the total bugs by .75. Your bugs should be integers because you do not have partial bugs. However, because we are dividing your result may be a double. To take care of this problem, put (int)(your formula) in your method. This is a cast. Be sure and put your formula in ().

Create a method to return the number of bugs.

Create a BugTester class

Create a main

Create a Bug object and begin with 10 bugs. Hint: Remember how you created a BankAccount Object with an initial balance.

Call / invoke the breedBugs method . Hint: Call deposit but no parameters.

Call the sprayBugs method 3 .

Print the number of bugs (The result should be an integer no half dead bugs)

Repeat j l two additional times. This means that you will breed, spray and print 3 times.

4. This was one of the problems from the first assignment. Correct Change: Write a program to assist a cashier with determining correct change.

Create a Change Class.

Create 4 integer instance variable, one for quarters, dimes, nickels, pennies.

Create a constructor that does not have any parameters and sets each of the instance variables to 0.

Create 4 integer constant variables. One for quarters (ie. private static final int QUARTER_VALUE = 25. Do the same thing for dimes, nickels, and pennies.

Create a calculateQuarters method. This method should accept a single integer (amount) to serves as the argument as used as input to the method. Calculate the number of quarters by dividing amount by the value of quarters and assign it to the quarters instance variable. Use the QUARTER_VALUE variable in your calculations see page 137. Using modulus (see page 138-139 of the text) determine the remainder. The remainder should be returned to the calling program.

Create 3 more methods like the calculateQuarters for dimes, nickels and pennies.

Create 4 get methods. One to return the number of quarters, one for dimes, one for nickels, and one for pennies.

Create a ChangeTester

Import Scanner.

Create a Scanner object.

Prompt the user to enter the amount.

Create an integer variable named change and using Scanner get the input from the user.

Create an object of type Change.

Create an integer variable called remainder and set it to 0.

Call the calculateQuarters method and put the return value in the variable called remainder. Example: remainer = myObject.calculateQuaters(change);

Call the calculateDimes method and put the return value in the variable called remainder. In this case, when you call calculateDimes use remainder as the parameter.

Repeat sept p for nickels and pennies.

Call and print getQuarters(), getDimes(), getNickels(), and getPennies(). When printing identify each number printed. Example: Quarters: 2

5. In this program we are going to work with strings. This will be a fun one I hope.

Create a class called NameTester. (You will not need a class. This will have the main method in it).

Import Scanner

Create a Scanner object

Prompt for your first name.

Create a variable for your first and get the user input for your first name.

Prompt for your last name.

Create a variable for your last name and get the input for your last name.

Create a variable for a blank.

Create a variable for your full name and assign it your first and last name and a blank using the + operator.

Using the variable for your full name, use the substring method (use the table on page 159) as a guide) print out your name with one letter on each line. For example:

M

S

.

W

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions