In this set of exercises you will write your own code utilizing OOP methods. The first section gives you the names of the Classes, methods and what the function of the methods. The second section let's you decide what Classes and methods will be needed to solve the problem.
- Write a program that allows the user to enter two numbers. The output produced should be as shown below.
- Create a Class named EnterNum
- declare an int field variable named enteredNum
- add a constructor that sets the value of enteredNum to 0
- add a void method named setEnteredNum() that asks for an integer to be entered and then places the value into the field variable enteredNum
- add an int method named getEnteredNum() that returns the value of the field variable enteredNum
- Create a Class named Multiply
- declare an int field variable named product
- add a constructor that sets the value of product to 0
- add a void method using setProduct(int n1, int n2) that multiplies any 2 numbers sent to it as parameter values and places the result into the field variable product
- add an int method using getProduct() that returns the value in the field variable product
- Create a Class named Add
- declare an int field variable named sum
- add a constructor that sets the value of sum to 0
- add a void method using setSum(int n1, int n2) that adds any 2 numbers sent to it as parameter values and places the result into the field variable sum
- add an int method using getSum() that returns the value in the field variable sum
- Design a Class Problem1 with a main method that creates all of the necessary objects and prints the output as shown below.
Sample output:
Enter an integer: 5
Enter an integer: 8
5 * 8 = 40
5 + 8 = 13
- Enter a program asks the user to enter his/her name and favorite animal.
- Create a Class named GetUserName
- declare two String field variables. Name them firstName and lastName
- add a constructor that sets the values of firstName and lastName to ""
- add a void method named setFirstName() that asks for a first name to be entered and then places the name into the field variable named firstName
- add a void method named setLastName() that asks for a last name to be entered and then places the name into the field variable named lastName
- add a String method named getFirstName() that returns the value in the field variable firstName
- add a String method named getLastName() that returns the value in the field variable lastName
- Create a Class named GetFavAnimal
- declare a String field variable named animal
- add a constructor that sets the value of animal to ""
- add a void method named setAnimal() that asks for a favorite animal to be entered and then places the entry into the field variable named animal
- add a String method named getAnimal() that returns the value in the field variable animal
- Design a Class named Problem2 with a main method that creates all of the necessary objects and prints the output as shown below.
Sample output:
Enter your first name: Jim
Enter your second name: Smith
Enter your favorite animal: dog
Hello Jim. My last name is also Smith and my favorite animal is also a dog.
- Write a program that allows the user to enter your weight in pounds and your height in inches. The output produced should be as shown below.
- Create a Class named EnterValues
- declare two double field variables. Name them height and weight
- add a constructor that sets the values of height and weight to 0
- add a void method named setWeight() that asks for a number to be entered and then places the entry into the the field variable named weight
- add a void method named setHeight() that asks for a number to be entered and then places the entry into the the field variable named height
- add a double method named getWeight() that returns the value in the field variable weight
- add a double method named getHeight() that returns the value in the field variable height
- Create a Class named DivideNumbers
- declare a double field variable named quotient
- add a constructor that sets the field variable quotient to 0
- add a void method named setQuotient(double w, double h) that divides two numbers sent to it as value parameters and places the result into the field variable quotient
- add a double method named getQuotient() that returns the value in the field variable quotient
- Create a Class named Problem3 with a main method that creates all of the necessary objects and prints the output as shown below.
Sample output:
Enter your weight in pounds: 150
Enter your height in inches: 75
You weigh 2.0 pounds per inch!
- Write a program that asks for the measurements of a room and then calculates the volume. (volume = length * width * height)
- Create a Class named GetDimensions
- declare three int field variables named height, width and length
- add a constructor that sets the values of the field variables to 0
- add a void method named setHeight() that asks for the height to be entered and then places the value in the field variable height
- add a void method named setWidth() that asks for the width to be entered and then places the value in the field variable width
- add a void method named setLength() that asks for the length to be entered and then places the value in the field variable length
- add an int method named getHeight() that returns the value in the field variable height
- add an int method named getWidth() that returns the value in the field variable width
- add an int method named getLength() that returns the value in the field variable length
- Create a Class named CalcVolume
- declare an int field variable named volume
- add a constructor that sets the value of the field variable volume to 0
- add a void method using setVolume(int H, int W, int L) to calculate the volume using three numbers sent as parameter values. Place the results into the field variable named volume.
- add an int method named getVolume() that returns the value in the field variable volume
- Create a Class named Problem4 with a main method that creates all of the necessary objects and prints the output as shown below.
Sample output:
Enter the room’s height (in feet): 10
Enter the room’s width (in feet): 12
Enter the room’s length (in feet): 8
The volume of the room is 960 cubic feet.
- The area of a triangle can be found by multiplying 0.5 times the base times the height. Write a program that allows the user to enter the base and height of a triangle and then displays the area.
- Create a Class named TriangleValues
- declare two int field variables named base and height
- add a constructor that sets the values of the field variables to 0
- add a void method named setHeight() that asks for the height of the triangle to be entered and then places the entry into the filed variable height
- add a void method named setBase() that asks for the base of the triangle to be entered and then places the entry into the field variable base
- add an int method named getHeight() that returns the value in the field variable height
- add an int method named getBase() that returns the value in the field variable base
- Create a Class named TriangleArea
- declare a double field variable named area
- add a constructor that sets the value of the field variable to 0.0
- add a void method using setArea(int b, int h) that uses two numbers sent as value parameters to calculate the area of a triangle and then places the results in the field variable named area
- add a double method named getArea() that returns the value of the field variable area
- Create a Class named Problem5 with a main method that creates all of the necessary objects and prints the output as shown below.
Sample output:
Enter the base (inches): 20
Enter the height (inches): 10
The triangle’s area is 100.0 square inches.
- Write a program that asks for two numbers to be entered. Have the computer divide the first number by the second number. You will need to use a do while() loop in the main method to verify that the numbers entered are valid. The computer output should be as shown below.
- Create a Class named EnterNumbers
- declare an int field variable named num
- add a constructor that sets the value of the field variable to 0
- add a void method using setNum(int firstOrSecond) that displays either Enter a whole number: or Enter another whole number (smaller than the first): depending on whether a 1 or a 2 was passed to it as a value parameter. The value entered should be placed in the field variable named num.
- add an int method named getNum() that returns the value in the field variable num
- Create a Class named VerifyInput
- add a boolean method using compareNums(int n1, int n2) that returns true if the second number entered is less than the first number entered and false if it is not
- Create a Class named WholeDivider
- declare two int field variables. Name them whole and remainder
- add a constructor that sets the value of the field variables to 0
- add a void method named setWhole(int n1, int n2) that divides two integers sent to it as value parameters and calculates the quotient. The result will be placed in the field variable named whole
- add a void method named setRemainder() that that divides two integers sent to it as value parameters and calculates the remainder. (Hint: use % to find the remainder.) The result will be placed in the field variable named remainder
- add an int method named getWhole() that returns the value in the field variable whole
- add an int method named getRemainder() that returns the value in the field variable remainder
- Create a Class named Problem6 with a main method that creates all of the necessary objects and prints the output as shown below.
Sample output:
Enter a whole number: 22
Enter another whole number (smaller than the first number): 37
Invalid entry. Second number must be less than the first.
Enter another whole number (smaller than the first number): 5
22 divided by 5 is 4 with a remainder of 2
- A flower company wants a program to calculate how many boxes it will take to ship its flower pot orders. You have been hired to write a program that calculates the number of boxes based on the following information. A large box can hold 4 pots and a small box can hold 1 pot. The output should be as shown below.
- Create a Class named EnterPots
- declare an int field variable named pots
- add a constructor that sets the value of the field variable to 0
- add a void method named setPots() that asks for the number of pots to be entered and places the entry into the field variable named pots
- add an int method named getPots() that returns the value in the field variable named pots
- Create a Class named CalcBoxes
- declare four int field variables named largeSize, smallSize, largeBoxes and smallBoxes
- add a constructor that sets the value of the field variable largeSize to 4, smallSize to 1, largeBoxes to 0 and smallBoxes to 0
- add a void method named setLargeBoxes(int potsOrdered) that divides the number of pots ordered by the value in the field variable named largeSize
- add a void method named setSmallBoxes(int potsOrdered) that divides the number of pots ordered by the value in the field variable named largeSize and then divides the remainder by the value in the field variable named smallSize
- add an int method named getLargeBoxes() that returns the value in the field variable named largeBoxes
- add an int method named getSmallBoxes() that returns the value in the field variable named smallBoxes
- Create a Class named Problem7 with a main method that creates all of the necessary objects and prints the output as shown below.
Sample output:
Enter the number of flower pots ordered: 27
You will need 6 large boxes and 3 small boxes.
- The flower company in the previous problem has a new set of boxes and needs you to modify the program you wrote. The new box sizes are as follows: a large box holds 8 pots, a medium box holds 4 pots and a small box holds 1. The output should be as shown below.
- In the CalcBoxes Class:
- add two int field variables to the CalcBoxes Class. Name them mediumSize and mediumBoxes
- in the constructor for the CalcBoxes Class set the value of the field variable mediumSize to 4
- in the constructor for the CalcBoxes Class set the value of the field variable mediumBoxes to 0
- in the constructor for the CalcBoxes Class set the value of the field variable largeSize to 8
- add a void method named setMediumBoxes() to the CalcBoxes Class that will determine the number of medium size boxes needed to ship the pots. Use the formula mediumBoxes=potsOrdered%largeSize/mediumSize;
- add an int method named getMediumBoxes() that returns the value in the field variable named mediumBoxes
- In the Problem7 Class
- Make modifications as needed to produce the correct output.
Sample output:
Enter the number of flower pots ordered: 71
You will need 8 large boxes, 1 medium box and 3 small boxes.
- Write a program that returns values from 1 to 15, 15 to 20 and 75 to 76 selected randomly by the computer.
When calculating random numbers use this formula: x = (int)(Math.random() * range) + lowest number possible;
where the range is calculated with the formula: highest number possible – lowest number possible + 1
- Create a Class named RandomNumGenerator
- add an int field variable named randomNumber
- add a constructor that sets the value of the field variable to 0
- add a void method using setRandomNumber(int max, int min) that generates a random number determined by the parameter values in max and min. The generated number will be placed in the field variable named randomNumber
- add an int method named getRandomNumber() that returns the value in the field variable named randomNumber
- Create a Class named Problem8 with a main method that creates all of the necessary objects and prints the output as shown below.
Sample output:
The number selected from 1 to 15 is 12
The number selected from 15 to 20 is 16
The number selected from 75 to 76 is 76
Note: For the rest of the problems, it is up to you to decide what Classes, constructors and methods are needed to solve the problem.
- Write a program that asks the user to enter a number from 1 to 100. The computer should check to see if the number falls in the desired range.
Sample output:
Enter a number from 1 to 100: 564
That number is not valid.
- Write a program that asks the user to in put a positive number to be used in calculating the area of a square. The computer should either calculate the area and display the answer or if the number entered is negative, respond with ‘Invalid entry’.
Sample output:
Enter a positive whole number: 20
The area of the square is 400 units
- Write a program that asks the user for the length, width, and height of a package, and displays “Reject” if any of dimension is greater than 10 cm, and “Accept” if all the dimensions are less than or equal to 10 cm.
- Write a program that allows two names to be entered. Have the computer compare them and print a message stating whether the first name entered is greater than, equal to, or less than the second name entered. The output should be as shown below. (Hint: use the compare.To method())
Sample output:
Enter a name: Sue
Enter another name: Joe
Sue comes after Joe alphabetically.
- Use the Switch statement to write a program that produces the following output. (four samples shown)
Sample output:
Enter a number from 1 to 5: 3
You will eat a banana.
Sample output:
Enter a number from 1 to 5: 2
You will eat an orange.
Sample output:
Enter a number from 1 to 5: 5
You will eat an apple.
Sample output:
Enter a number from 1 to 5: 12
That is an invalid response
- In a for loop have the computer generate 10 random numbers ranging from 1 to 10. The computer should print whether the numbers are less than 5, greater than 5 or equal to 5.
Sample output:
On turn 1 the number 6 was chosen and found to be greater than 5.
On turn 2 the number 5 was chosen and found to be equal to 5.
…
On turn 10 the number 2 was chosen and found to be less than 5.
- Write a program that simulates the rolling of a pair of dice. The computer should keep on rolling until a 12 is rolled.
Sample output:
Roll 1 = 5
Roll 2 = 7
Roll 3 = 12
A 12 was rolled, program terminated.
- Write a program that has the computer store in an array 10 randomly chosen numbers ranging from 1 to 100. The computer should then print a list of the even numbers followed by a list of the odd numbers.
Sample output:
Even numbers: 4 88 62 46
Odd numbers: 57 97 11 73 15 3
- Write a program that begins randomly choosing numbers ranging from 1 to 50. Have the computer stop choosing numbers when that number is a repeat.
Sample output:
33 8 28 92 25 46 28
28 was stored at position 3 and at position 7.
- Write a method that prompts the user to enter his or her first and last names and then displays the initials of the name in uppercase no matter the case used in the entry.
Sample output:
Enter your first name: joe
Enter your last name: smith
Your initials are: JS
- Write a method that prompts the user to enter his or her first, middle, and last names and then displays a monogram with the first and middle initials in lowercase and the last initial in uppercase.
Sample output:
Enter your whole name: Joe William Smith
Your monogram is: jSw
|
|
|