This document contains the code for an integration project that provides a menu-driven program for completing various Java exercises related to variables, operators, arithmetic, flow control, methods, repetition, exceptions, OOP, arrays, strings, collections, and other Java topics. The code displays a menu, gets user input to select an exercise, and contains implementation code for 41 different exercises that is executed based on the user's selection.
1. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
import java.util.*;
import java.io.*;
import java.util.AbstractCollection;
public class sampleExercise {
public static void main(String[] args) {
// Primitive Data Types
// byte - 8 bit signed two’s complement integer
// Min value of -128 and max value of 127
// Useful for saving memory in large arrays
// Can be used in place of int
// short - 16 bit signed two’s complement integer
// Min value of -32,768 and max value of 32,767
// Can save memory the same way a byte can
// int - 32 bit signed two’s complement integer
// Min value of -2^31 and a Max value of 2^31 - 1
// long - 64 bit two’s complement integer
// Min value of -2^63 and Max value of 2^63
// Use when you need a range of values wider than what int can provide
// float - Single-precision 32 bit IEEE 754 floating point
// Range of values are beyond the scope of discussion
// Use if you need to save memory in large arrays of floating point
// numbers
// double - Double-precision 64 bit IEEE 754 floating point
// Range of values are also beyond the scope of discussion
// Usually the default choice for decimal values
// Never should be used to represent currency
// boolean - Has only two possible values: true and false
// Use for simple flags that track true and false conditions
// Represents one bit of information
// char - Single 16 bit Unicode character
// Min value of 0 and Max value of 65,535
int exercise;
Scanner keyboard = new Scanner(System.in);
System.out.println("1. First Variables Exercise.");
System.out.println("2. Second Variables Exercise.");
System.out.println("3. Third Variables Exercise.");
System.out.println("4. First Operators Exercise.");
System.out.println("5. Second Operators Exercise.");
System.out.println("6. Third Operators Exercise.");
System.out.println("7. First Arithmetic Exercise.");
System.out.println("8. Second Arithmetic Exercise.");
System.out.println("9. Third Arithmetic Exercise.");
System.out.println("10. First Flow Control Exercise.");
System.out.println("11. Second Flow Control Exercise.");
System.out.println("12. Third Flow Control Exercise");
System.out.println("13. First Methods Exercise");
-1-
2. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
System.out.println("14. Second Methods Exercise");
System.out.println("15. Third Methods Exercise");
System.out.println("16. First Repetition Exercise ");
System.out.println("17. Second Repetition Exercise");
System.out.println("18. Third Repetition Exericise");
System.out.println("19. First Exceptions Exercise");
System.out.println("20. Second Exceptions Exercise");
System.out.println("21. First OOP Exercise");
System.out.println("22. Second OOP Exercise");
System.out.println("23. Third OOP Exercise");
System.out.println("24. First Array Exercise");
System.out.println("25. Second Array Exercise");
System.out.println("26. Third Array Exercise");
System.out.println("27. First Strings Exercise");
System.out.println("28. Second Strings Exercise");
System.out.println("29. Third Strings Exercise");
System.out.println("30. First Collections Exercise");
System.out.println("31. Second Collections Exercise");
System.out.println("32. Third Collections Exercise");
System.out.println("33. First Miscellaneous Exercise");
System.out.println("34. Second Miscellaneous Exercise");
System.out.println("35. Third Miscellaneous Exercise");
System.out.println("36. First Polymorphism Exercise");
System.out.println("37. Second Polymorphism Exercise");
System.out.println("38. Third Polymorphism Exercise");
System.out.println("39. First Abstract Class Exercise");
System.out.println("40. Second Abstract Class Exercise");
System.out.println("41. Third Abstract Class Exercise");
System.out.println("rn");
System.out.println("Enter your selection:");
exercise = keyboard.nextInt();
if (exercise < 1 || exercise > 41) {
System.out.println("Your selection is invalid.");
System.out.println("Enter any number between 1 and 41.");
-2-
3. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
exercise = keyboard.nextInt();
}
switch (exercise) {
// Variable - a variable is, depending on its data type, a word,
// partial
// word, acronym,etc., that allows the user to store values to
// utilize
// in his/her program in order to save space and make the program
// more
// efficient as well as more organized.
case 1:
// First Variable Exercise
String greeting;
greeting = "Hello Martians";
int age = 21; // Variable "age" has an integer value
System.out.println(greeting + " I am " + age + " years old");
break;
case 2:
// Second Variable Exercise
boolean isGood = true; // "isGood" is a boolean value that =
// true
// "pMartian is a string
String pMartian = "This universe is basically good";
System.out.println(pMartian + " - " + isGood);
break;
case 3:
// Third Variable Exercise
boolean affirmative = true;
System.out.println(affirmative);
break;
case 4:
try {
// First Operators Exercise - arithmetic operators
int a, b; // Integer values will be stored in "a" and "b"
int resulta;
int results;
int resultm;
float resultd; // Result for resultd will not be an integer, but
// a
// decimal value
System.out.print("Enter a:"); // Asks user for input
a = keyboard.nextInt(); // User input stored in "a"
System.out.print("Enter b:");
-3-
4. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
b = keyboard.nextInt(); // User input stored in "b"
resulta = a + b;
results = a - b;
resultm = a * b;
resultd = (float) a / b; // float needed to get correct
// quotient.
// Integer Division will give you an integer answer, however, if
// your
// numerator happens to not be equally divisible by the
// denominator,
// you
// will need to cast the process using the appropriate data type
// to
// get
// the correct answer
// Now all calculations using "a" and "b" are printed below
System.out.println("The result of adding is " + resulta);
System.out.println("The result of subtracting is " + results);
System.out.println("The result of multiplying is " + resultm);
System.out.println("The result of dividing is " + resultd);
} catch (InputMismatchException e2) {
System.out.println("Please enter an integer.");
}
break;
case 5:
try {
// Second Operators Exercise - Celsius to Farhenheit Converter
System.out.println("Enter a temperature in Celsius: ");
double Fahrenheit = 0;// Gives Fahrenheit an initial value of 0
// keyboard.nextDouble() being included after = makes Celsius
// take
// value
// of user
// input
double Celsius = keyboard.nextDouble();
// Now below you have to use a formula for the temp conversion
Fahrenheit = ((Celsius * 9 / 5) + 32);
System.out.println("The temperature in Fahrenheit is: " + Fahrenheit);
} catch (InputMismatchException e2) {
System.out.println("Please enter a number.");
}
break;
case 6:
-4-
5. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
try {
// Third Operators Exercise - Largest of Three Numbers
int x, y, z;// variables declared
System.out.println("Enter three integers:");// user input is
// stored
// in 3
// separate
// variables
x = keyboard.nextInt();
y = keyboard.nextInt();
z = keyboard.nextInt();
if (x > y && x > z && z != y) { // if statements sort through
// all of
// the
// possible values the user can
// enter
// and gives an output based on
// what
// is
// entered
System.out.println("First number is largest.");
}
if (y > x && y > z && x != z) {
System.out.println("Second number is largest.");
}
if (z > x && z > y && x != y) {
System.out.println("Third number is largest.");
}
else {
System.out.println("Entered numbers are not distinct.");// If
// none
// of
// these
// conditions
// are
// true,
// then
// this
// will
// be
// the
// output
}
} catch (InputMismatchException e2) {
System.out.println("Please enter an integer.");
}
break;
case 7:
try {
// First Arithmetic Exercise - Absolute Equals
-5-
6. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
boolean equal = true; // boolean value can only take the value
// of
// true
// or false
System.out.println("Enter the 1st number:");
int num1 = keyboard.nextInt(); // num1 takes value of whatever
// the
// user
// entered first
System.out.println("Enter the 2nd number:");
int num2 = keyboard.nextInt(); // num2 takes value of whatever
// the
// user
// entered second
if (((Math.abs(num1)) != (Math.abs(num2)))) {// Math.abs finds
// the
// absolute value
// of
// the
// user input
equal = false;
System.out.println("Result is:" + equal);
}
equal = true;// same as having an else statement, if the other
// conditions above are not met, this will be the
// output
System.out.println("Result is:" + equal);
} catch (InputMismatchException e2) {
System.out.println("Please enter an integer.");
}
break;
case 8:
try {
// Second Arithmetic Exercise - Floor and Ceiling of a Number
System.out.println("Enter a number:");
double dob = keyboard.nextDouble(); // double means the value
// will
// not be
// an integer, but a decimal
// value
// instead
System.out.print((int) Math.ceil(dob)); // Math.ceil will find
// the
// ceiling of a number
System.out.println("rn"); // Another way to space between
// outputs
System.out.print((int) Math.floor(dob)); // Math.floor will find
// the
// floor of a number
} catch (InputMismatchException e2) {
-6-
7. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
System.out.println("Please enter an integer.");
}
break;
case 9:
try {
// Third Arithmetic Exercise - Square Root, Quadratic Root
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number:");
double s = scan.nextDouble();
System.out.printf("%.5f", Math.sqrt(s));// Math.sqrt takes the
// square
// root of a number
System.out.print("rn");
System.out.printf("%.5f", Math.pow(s, .25));// Math.pow and the
// value
// .25 takes the
// quadratic
// root of a number
System.out.println("rn");
} catch (InputMismatchException e2) {
System.out.println("Please enter an integer.");
}
break;
case 10:
try {
// First Flow Control Exercise - Right or Wrong
System.out.println("What is 12+12?");
boolean correct = false;
System.out.println();
int answer = keyboard.nextInt();
if (answer == 24) { // Only if the user input equals 24 will the
// variable correct be equal to true
correct = true;
}
System.out.println(correct);// Otherwise, the output will be
// false
} catch (InputMismatchException e2) {
System.out.println("Please enter an integer.");
}
break;
case 11:
try {
// Second Flow Control Exercise - Sleep
System.out.print("Is it a weekday today?(true or false):");
-7-
8. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
boolean w = keyboard.nextBoolean(); // w takes the value of the
// user
// input, but has to be
// eiter
// true or
// false
System.out.print("Are you on a vacation?(true or false):");
boolean v = keyboard.nextBoolean();
if (w == true && v == false) {
System.out.println("You cannot sleep.");// If you are ever
// on
// vacation, you can
// sleep
}
else if (w == false && v == true) {
System.out.println("You can sleep.");
}
else if (w == true && v == true) {
System.out.println("You can sleep.");
}
else {
System.out.println("You can sleep.");
}
} catch (InputMismatchException e2) {
System.out.println("Please enter true or false.");
}
break;
case 12:
try {
// Third Flow Control Exercise - Lucky Sum
int d, e, f, g;
System.out.println("Enter 3 values:");
d = keyboard.nextInt();
e = keyboard.nextInt();
f = keyboard.nextInt();
g = d + e + f;
if (d == 13) {
System.out.println("Lucky sum:" + 0);// If any value entered
// equals
// 13, then that number
// as
// well
// as all numbers to
// the
// right
// of it will not count
// towards
// the sum
}
else if (d != 13 && e == 13) {
-8-
9. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
System.out.println("Lucky sum:" + d);
}
else if (d != 13 && e != 13 && f == 13) {
System.out.println("Lucky sum:" + (d + e));
}
else {
System.out.println("Lucky sum:" + g); // If not numbers
// entered
// are
// equal to 13, then
// the
// sum
// will be calculated
// normally
}
} catch (InputMismatchException e) {
System.out.println("Please enter an integer.");
}
break;
case 13:
try {
// First Methods Exercise
System.out.println("Enter a Number:");
double number = keyboard.nextDouble();
number = (Math.sqrt(number));
int sqrt = ((int) Math.round(number));
System.out.println("The square root is:" + sqrt);
} catch (InputMismatchException e) {
System.out.println("Please enter an integer.");
}
break;
case 14:
try {
// Second Methods Exercise
System.out.println("Enter first number:");
int x1 = keyboard.nextInt();
System.out.println("Enter second number:");
int y1 = keyboard.nextInt();
boolean result = false;
if (y1 % x1 == 0) {
result = true;
System.out.println(result);
}
else {
System.out.println(result);
}
} catch (InputMismatchException e) {
-9-
10. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
System.out.println("Please enter an integer.");
}
break;
case 15:
try {
// Third Methods Exercise
System.out.println("Enter the radius:");
int r = keyboard.nextInt();
double pi = 3.14;
double area = (pi * (r * r));
System.out.println("Area of circle is:" + area);
} catch (InputMismatchException e) {
System.out.println("Please enter an integer.");
}
break;
case 16:
try {
// First Repetition Exercise
int num;
int sum = 0;
int count = 1;
while (count < 6) {
System.out.println(" Enter Number " + count);
num = keyboard.nextInt();
sum = num + sum;
count++;
}
System.out.println(sum);
} catch (InputMismatchException e) {
System.out.println("Please enter an integer.");
}
break;
case 17:
try {
// Second Repetition Exercise
System.out.println("Enter a number:");
int n = 0;
for (int lsum = 0; n != 0; lsum++) {
n = keyboard.nextInt();
lsum = lsum + n;
System.out.println("The total is:" + lsum);
-10-
11. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
}
} catch (InputMismatchException e) {
System.out.println("Please enter any integer but zero.");
}
break;
case 18:
try {
// Third Repetition Exercise
int factorial = 1;
int currentValue = 1;
System.out.println("Enter a Number.");
int input = keyboard.nextInt();
while (currentValue <= input) {
factorial *= currentValue;
currentValue++;
}
System.out.println("The Factorial of " + input + " is " + factorial);
} catch (InputMismatchException e) {
System.out.println("Please enter an integer.");
}
break;
case 19:
// First Exceptions Exercise
String id = "";
int idnum;
System.out.println("Enter the ID number:");
id = keyboard.next();
try {
idnum = Integer.parseInt(id);
if (id.length() != 10) {
throw new NumberFormatException();
}
System.out.println("correct");
}
catch (NumberFormatException e1) {
System.out.println("incorrect");
} catch (Exception e1) {
System.out.println("incorrect");
}
break;
case 20:
-11-
12. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
// Second Exceptions Exercise
try {
System.out.println("Enter a number:");
int value = keyboard.nextInt();
if (value % 5 == 0 && value % 2 == 0) {
// This relational operator between two numerical values
// relates them with the equals operator
// Also, the && is a conditional operator that signifies
// that if both of the conditions mentioned aren't true,
// then its false
// A conditional operator pretty much combines
// conditions to
// further modify an if statement
System.out.println("Correct");
}
else if (value == 5) {
Exception e1 = new Exception();
throw e1;
}
}
catch (Exception e1) {
System.out.println("wrong");
}
break;
case 21:
// First OOP Exercise
Person person1 = new Person();
person1.setInfo("Test", 25);// this is an example of a method
// call
System.out.println(person1.getInfo());
break;
case 22:
// Second OOP Exercise
try {
System.out.println("Enter name:");
String somename = keyboard.nextLine();
System.out.println("Enter email:");
String semail = keyboard.nextLine();
System.out.println("Enter book name:");
String sbook = keyboard.nextLine();
Author author1 = new Author();
author1.setStuff(somename, semail, sbook);// What is in
// parenthesis
// after
-12-
13. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
// the method name here are
// considered arguments
System.out.println(author1.getName());
System.out.println(author1.getEmail());
System.out.println(author1.getBook());
} catch (InputMismatchException e) {
System.out.println("Please enter a string.");
}
break;
case 23:
try {
// Third OOP Exercise
System.out.println("Enter radius:");
double rad = keyboard.nextDouble();
System.out.println("Enter color:");
String col = keyboard.next();
Area mc = new Area();
mc.setArea(rad);
mc.setColor(col);
System.out.println(mc.getArea());
System.out.println(mc.getColor());
} catch (InputMismatchException e) {
System.out.println("Please enter an integer for radius and a string for color.");
}
break;
case 24:
try {
System.out.print("Enter a string:");
String con = keyboard.next();
char[] str = con.toCharArray();
int len = str.length;
for (int i = len - 1; i >= 0; i--) {
System.out.print(str[i]);
}
} catch (InputMismatchException e) {
System.out.println("Please enter a string.");
}
break;
case 25:
try {
int summ = 0;
int sumElements[];
sumElements = new int[10];
System.out.println("Enter elements of array :");
for (int i = 0; i < 10; i++) {
sumElements[i] = keyboard.nextInt();
summ = summ + sumElements[i];
}
System.out.println("Sum of elements of the array:" + summ);
-13-
14. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
} catch (InputMismatchException e) {
System.out.println("Please enter integers.");
}
break;
case 26:
try {
int productArray[];
int max = 0;
int min = 0;
productArray = new int[10];
System.out.println("Enter elements of array :");
for (int i = 0; i < 10; i++) {
productArray[i] = keyboard.nextInt();
for (int j = 0; j < i; j++) {
if (productArray[j] > productArray[j + 1]) {
int temp = productArray[j];
productArray[j] = productArray[j + 1];
productArray[j + 1] = temp;
max = productArray[9];
min = productArray[0];
}
}
}
System.out.println("Result is :" + (max * min));
} catch (InputMismatchException e) {
System.out.println("Please enter integers.");
}
break;
case 27:
try {
System.out.println("Enter an Uppercase letter:");
String userInput = keyboard.next();
String low = userInput.toLowerCase();
System.out.println(low);
} catch (InputMismatchException e) {
System.out.println("Please enter a string.");
}
break;
case 28:
try {
System.out.println("Enter a string:");
String input1 = keyboard.next();
char[] inputArray = input1.toCharArray();
int input1Length = inputArray.length;
System.out.println("Length of the string is:" + input1Length);
} catch (InputMismatchException e) {
System.out.println("Please enter a string.");
}
-14-
15. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
break;
case 29:
try {
System.out.print("Enter a line of text: ");
String line = keyboard.nextLine();
String upperCa = line.toUpperCase();
System.out.println(upperCa);
} catch (InputMismatchException e) {
System.out.println("Please enter a string.");
}
break;
case 30:
try {
System.out.println("Enter the number of integers in your list.");
int numb = keyboard.nextInt();
int[] bubbleArray = new int[numb];
for (int i = 0; i < numb; i++) {
bubbleArray[i] = keyboard.nextInt();
for (int j = 0; j < i; j++) {
if (bubbleArray[j] > bubbleArray[j + 1]) {
int val = bubbleArray[j];
bubbleArray[j] = bubbleArray[j + 1];
bubbleArray[j + 1] = val;
}
}
}
System.out.println(bubbleArray[bubbleArray.length - 1]);
} catch (InputMismatchException e) {
System.out.println("Please enter an integer.");
}
break;
case 31:
try {
System.out.println("Enter the 1st number:");
int numm1 = keyboard.nextInt();
System.out.println("Enter the 2nd number:");
int numm2 = keyboard.nextInt();
System.out.println("Enter the 3rd number:");
int numm3 = keyboard.nextInt();
Stack<Integer> list1 = new Stack<Integer>();
System.out.println("Stack:[]");
list1.push(numm1);
System.out.println(list1);
list1.push(numm2);
System.out.println(list1);
list1.push(numm3);
System.out.println(list1);
list1.pop();
System.out.println(list1);
} catch (InputMismatchException e) {
System.out.println("Please enter an integer.");
}
break;
-15-
16. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
case 32:
try {
ArrayList<Integer> al = new ArrayList<Integer>();
System.out.println("Enter the number of integers that will be inserted: ");
int size = keyboard.nextInt();
System.out.println("Enter some integers to insert to the ArrayList: ");
while (size-- > 0) {
al.add(keyboard.nextInt());
}
int maxim = Collections.max(al);
int maxindex = al.lastIndexOf(maxim);
System.out.println("The largest value is " + maxim + "; which " + "is in slot " +
maxindex);
} catch (InputMismatchException e) {
System.out.println("Please enter an integer.");
}
break;
case 33:
try {
String u1 = keyboard.next();
char[] pal = u1.toCharArray();
Palindrome goTo = new Palindrome();
if (goTo.isItPalindrome(pal)) {
System.out.println("palindrome");
}
else {
System.out.println("not a palindrome");
}
} catch (InputMismatchException e) {
System.out.println("Please enter a string.");
}
break;
case 34:
try {
System.out.println("Enter a number:");
String possibleBiNumber = keyboard.next();
int n1 = possibleBiNumber.charAt(0);
if (possibleBiNumber.matches("[01]+") == true) {
System.out.println("binary");
}
else {
System.out.println("not binary");
}
} catch (InputMismatchException e) {
System.out.println("Please enter an integer.");
}
break;
-16-
17. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
case 35:
try {
String ones[] = { "one", "two", "three", "four", "five", "six", " seven", "eight",
"nine", "ten", "eleven",
"twelve", "thirteen", "forteen", "fifteen", "sixteen", "seventeen", "eighteen",
"ninteen" };
String tens[] = { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy",
"eighty", "ninety" };
int digitInput;
System.out.println("Enter the number:");
digitInput = keyboard.nextInt();
char[] digitArray = String.valueOf(digitInput).toCharArray();
int tensNumber = Character.getNumericValue(digitArray[0]);
int onesNumber = Character.getNumericValue(digitArray[1]);
if (digitInput >= 1 && digitInput <= 19) {
System.out.println("Entered number is:" + ones[digitInput - 1]);
}
else if (digitInput >= 20) {
if (onesNumber == 0) {
System.out.println("Entered number is:" + tens[tensNumber - 2]);
}
else {
System.out.println("Entered number is:" + tens[tensNumber - 2] + " " +
ones[onesNumber - 1]);
}
}
else {
System.out.println("Your input was invalid, please enter an" + " integer");
}
} catch (InputMismatchException e) {
System.out.println("Please enter an integer.");
}
break;
case 36:
try {
Box mybox1 = new Box();
Box mybox2 = new Box();
Box mycube = new Box();
int vol;
vol = mybox1.volume(10, 20, 15);
System.out.println("Volume of mybox1 is " + vol);
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
vol = mycube.volume(7);
-17-
18. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
System.out.println("Volume of mycube is " + vol);
} catch (InputMismatchException e) {
System.out.println("Please enter an integer.");
}
break;
case 37:
Overload overload = new Overload();
int endResult;
String endResult2;
char endResult3;
endResult = overload.test(10);
System.out.println("a" + " " + endResult);
endResult2 = overload.test("10 20");
System.out.println("a and b" + " " + endResult2);
endResult3 = overload.test('a');
System.out.println("char" + " " + endResult3);
endResult = 97;
System.out.println("Result" + " " + endResult);
break;
case 38:
Animal movement = new Animal();
Dog typeOfMovement = new Dog();
movement.move();
System.out.println("n");
typeOfMovement.move();
break;
case 39:
Counter loopIncrement = new Counter();
loopIncrement.increment();
loopIncrement.increment();
loopIncrement.increment();
loopIncrement.print();
break;
case 40:
System.out.println("Enter the radius:");
double radius = keyboard.nextDouble();
Circle myCircle = new Circle();
myCircle.setArea(radius);
System.out.println("Radius is:" + radius + "and Area is:" + myCircle.getArea());
break;
case 41:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter name:");
String name1 = scanner.nextLine();
System.out.println("Enter unit price:");
double up = scanner.nextDouble();
System.out.println("Enter weight:");
-18-
19. C:Userslildg_000OneDriveN++ CodeIntegration Project.java Saturday, April 16, 2016 12:09 AM
double w = scanner.nextDouble();
WeighedItem item1 = new WeighedItem(name1, up, w);
item1.setPrice(up, w);
scanner.nextLine();
System.out.println("Enter name:");
String name2 = scanner.nextLine();
System.out.println("Enter unit price:");
double up1 = scanner.nextDouble();
System.out.println("Enter quantity:");
int q = scanner.nextInt();
CountedItem item2 = new CountedItem(name2, up1, q);
item2.setPrice(up);
System.out.println(item1);
System.out.println(item2);
break;
}
}
}
-19-
20. C:Userslildg_000OneDriveN++ CodeIntegration Project Method References.java Saturday, April 16, 2016 12:10 AM
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class Person {
private String name;
private int age;
public void setInfo(String n, int a) {
name = n;
age = a;
}
public String getInfo() {
return "Name=>" + name + " and Age=>" + age;
}
}
class Author {
private String name;
private String email;
private String book;
public void setStuff(String n, String e, String b) {
name = n;
email = e;
book = b;
}
public String getName() {
return name + "n";
}
public String getEmail() {
return email + "n";
}
public String getBook() {
return book + "n";
}
}
class Area {
private double area;
private String color;
private double pi = 3.141592653589793;
private double radius;
public void setArea(double r) {
radius = r;
}
public void setColor(String c) {
color = c;
}
public double getArea() {// A parameter is what is located
// inside of
// the parenthesis after the method name
-1-
21. C:Userslildg_000OneDriveN++ CodeIntegration Project Method References.java Saturday, April 16, 2016 12:10 AM
double area = (pi * (Math.pow(radius, 2)));
return area;
}
public String getColor() {
return color;
}
}
class Palindrome {
public boolean isItPalindrome(char[] userIn) {
int endLetter = userIn.length - 1;
int beginningLetter = 0;
while (endLetter > beginningLetter) {
if (userIn[endLetter] != userIn[beginningLetter]) {
return false;
}
--endLetter;
++beginningLetter;
}
return true;
}
}
class Box {
int width;
int height;
int depth;
public int volume(int w, int h, int d) {
width = w;
height = h;
depth = d;
return width * height * depth;
}
public int volume() {
width = -1;
height = 1;
depth = 1;
return width * height * depth;
}
public int volume(int w) {
width = w;
height = 49;
depth = 1;
return width * height * depth;
}
}
class Overload {
int output;
String output2;
-2-
22. C:Userslildg_000OneDriveN++ CodeIntegration Project Method References.java Saturday, April 16, 2016 12:10 AM
char output3;
public int test(int o) {
output = o;
return output;
}
public String test(String n) {
output2 = n;
return output2;
}
public char test(char a) {
output3 = a;
return output3;
}
}
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
class Counter {
int i = 0;
Counter increment() {
i++;
return this;
}
void print() {
System.out.println("i = " + i);
}
}
class Circle extends Area {
}
-3-
23. C:Userslildg_000OneDriveN++ CodePurchaseItem Class.java Saturday, April 16, 2016 12:10 AM
class PurchaseItem {
private String name;
private double unitPrice;
public PurchaseItem(String n, double up) {
name = n;
setPrice(up);
}
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
public void setPrice(double up) {
unitPrice = (up > 0) ? up : 0;
}
public double getPrice() {
return unitPrice;
}
public String toString() {
return getName() + "@ " + getPrice();
}
}
-1-
24. C:Userslildg_000OneDriveN++ CodeWeighedItem Class.java Saturday, April 16, 2016 12:10 AM
public class WeighedItem extends PurchaseItem {
private double Kg;
private double actualPrice;
public WeighedItem(String n, double up, double kg) {
super(n, up);
this.Kg = kg;
}
public void setPrice(double up, double kg) {
actualPrice = up * Kg;
}
public String toString() {
return super.getName() + " @ "+ super.getPrice() + " " + Kg + " Kg " + actualPrice + " $";
}
}
-1-
25. C:Userslildg_000OneDriveN++ CodeCountedItem Class.java Saturday, April 16, 2016 12:10 AM
public class CountedItem extends PurchaseItem {
private int quantity;
private double actualPrice;
public CountedItem(String n, double up, int quantity) {
super(n, up);
this.quantity = quantity;
}
public void setPrice(double up, int quantity) {
actualPrice = up * quantity;
}
public String toString() {
return super.getName() + " @ " + super.getPrice() + " " + quantity + " units " +
actualPrice + " $";
}
}
-1-