1. In Java, methods must include all of the following except a call to another method. (b)
2. All method declarations contain parentheses. (d)
3. A public static method named computeSum() is located in classA. To call the method from within classB, use the statement classA.computeSum(). (c)
4. Which of the following method declarations is correct for a static method named displayFacts() if the method receives an int argument? public static void displayFacts(int data) (c)
5. The method with the declaration public static int a Method(double d) has a method type of int. (b)
* The method type is its return type.
6. Which of the following is a correct call to a method declared as public static void aMethod(char code)? aMethod('Q');
7. A method is declared as public static void showResults(double d, int i). Which of the following is a correct method call? showResults(12.2, 67); (b)
8. The method with the declaration public static char procedure(double d) has a method type of char. (c)
9. The method public static boolean testValue(int response) returns a boolean value. (a)
10. Which of the following could be the last legally coded line of a method declared as public static int getVal (double sum)? return 77;
11. The nonstatic data components of a class often are referred to as the access types of that class. (a)
12. Objects contain methods and data items, which are also known as fields. (a)
13. You send message or information to an object through its methods. (b)
14. A program or class that instantiates objects of another prewritten class is a(n) client class. (a)
15. The body of a class is always written between curly braces. (c)
16. Most class data fields are private. (a)
17. The concepts of allowing a class's private data to be changed only by a class's own methods is known as information hiding. (c)
18. Suppose you declare an object as Book thisBook;. Before you store data in thisBook, you also must explicitly allocate memory for it. (a)
19. If a class is named Student, the class constructor name is Student. (d)
20. If you use the automatically supplied default constructor when you create an object, numeric fields are set to 0 (zero). (a)
Exercises
public class TestMethods /* Introduction exercise to methods, do what the method identifiers state */ { public static void main(String[] args) { int valueOne = 1; int valueTwo = 2; displayIt(valueOne); displayIt(valueTwo); displayItTimesTwo(valueOne); displayItTimesTwo(valueTwo); displayItPlusOneHundred(valueOne); displayItPlusOneHundred(valueTwo); } public static void displayIt(int val) /* Displays the value */ { System.out.println(val); } public static void displayItTimesTwo(int val) /* Multiplies value by 2 */ { System.out.println(val * 2); } public static void displayItPlusOneHundred(int val) /* Adds 100 to the value */ { System.out.println(val + 100); } }
public class Numbers { public static void main(String[] args) { int valueOne = 1; int valueTwo = 2; sum(valueOne, valueTwo); difference(valueOne, valueTwo); } public static void sum(int valOne, int valTwo) { System.out.println(valOne + " + " + valTwo + " = " + (valOne + valTwo)); } public static void difference(int valOne, int valTwo) { System.out.println(valOne + " - " + valTwo + " = " + (valOne - valTwo)); } }
public class Numbers2 { public static void main(String[] args) { int valueOne = 1; int valueTwo = 2; sum(valueOne, valueTwo); difference(valueOne, valueTwo); int productResult = product(valueOne, valueTwo); System.out.println(valueOne + " * " + valueTwo + " = " + productResult); } public static void sum(int valOne, int valTwo) { System.out.println(valOne + " + " + valTwo + " = " + (valOne + valTwo)); } public static void difference(int valOne, int valTwo) { System.out.println(valOne + " - " + valTwo + " = " + (valOne - valTwo)); } public static int product(int valOne, int valTwo) { return (valOne * valTwo); } }
import java.util.Scanner; public class Eggs { public static void main(String[] args) { int numberOfEggs; Scanner keyBoard = new Scanner(System.in); System.out.print("Enter the number of eggs: "); numberOfEggs = keyBoard.nextInt(); displayEggs(numberOfEggs); } public static void displayEggs(int eggs) { int dozens = eggs/12; int remainder = eggs % 12; System.out.println(eggs + " eggs is " + dozens + " dozens (with " + remainder + " eggs remaining)"); } }
import java.util.Scanner; public class Exponent { public static void main(String[] args) { int value; Scanner keyBoard = new Scanner(System.in); System.out.print("Enter a value: "); value = keyBoard.nextInt(); int valueSquared = square(value); int valueCubed = cubed(value); System.out.println(value + " squared is " + valueSquared); System.out.println(value + " cubed is " + valueCubed); } public static int square(int val) { return (val * val); } public static int cubed(int val) { return (val * val * val); } }
import java.util.Scanner; public class Exponent2 { public static void main(String[] args) { int value; Scanner keyBoard = new Scanner(System.in); System.out.print("Enter a value: "); value = keyBoard.nextInt(); int valueSquared = square(value); int valueCubed = cubed(value); System.out.println(value + " squared is " + valueSquared); System.out.println(value + " cubed is " + valueCubed); } public static int square(int val) { return (val * val); } public static int cubed(int val) { return (square(val) * val); } }
import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner keyBoard = new Scanner(System.in); System.out.print("Enter the price of the item: "); double priceOfItem = keyBoard.nextDouble(); System.out.print("Enter the commission: "); double commission = keyBoard.nextDouble(); System.out.print("Enter the customer rate discount: "); double discount = keyBoard.nextDouble(); double finalPrice = calculateFinalPrice(priceOfItem, commission, discount); System.out.println("The final price is: " + finalPrice); } public static double calculateFinalPrice(double price, double salesCommission, double customerDiscount) { double priceWithCommission = price + (price * salesCommission); double totalDiscount= (customerDiscount * price); return (priceWithCommission - totalDiscount); } }
import java.util.Scanner; public class Divide { public static void main(String[] args) { Scanner keyBoard = new Scanner(System.in); System.out.print("Enter first value: "); int valOne = keyBoard.nextInt(); System.out.print("Enter second value: "); int valTwo = keyBoard.nextInt(); Calculate (valOne, valTwo); } public static void Calculate(int first, int second) { int result = (first / second); int remainder = first % second; System.out.println(first + " / " + second + " = " + result + " with a remainder: " + remainder); } }
import java.util.Scanner; public class Salary { public static void main(String[] args) { Scanner keyBoard = new Scanner(System.in); System.out.print("Enter hourly pay rate: "); double hourlyPayRate = keyBoard.nextDouble(); System.out.print("Enter regular hours: "); int regularHours = keyBoard.nextInt(); System.out.print("Enter overtime hours: "); int overtimeHours = keyBoard.nextInt(); double totalPay = CalculateOvertimePay(hourlyPayRate, regularHours, overtimeHours); System.out.println("The total pay is: " + totalPay); } public static double CalculateOvertimePay(double hourlyRate, double regHours, double overtime) { double normalPay = (hourlyRate * regHours); double overtimePay = (overtime * (1.5 * hourlyRate)); return (normalPay + overtimePay); } }
import java.util.Scanner; public class Interest { public static void main(String[] args) { double val = GetStartingValue(); double result = CalculateInvestment(val); System.out.println("After one year at 5% interest, you will have: $" + result); } public static double GetStartingValue() { Scanner keyBoard = new Scanner(System.in); System.out.print("Enter starting value of investment: "); double startingValue = keyBoard.nextDouble(); return startingValue; } public static double CalculateInvestment(double val) { return (val * 1.05); } }
public class Pizza { private String toppings; private int diameter; private double price; public String getToppings() { return toppings; } public void setToppings(String top) { toppings = top; } public int getDiameter() { return diameter; } public void setDiameter(int dia) { diameter = dia; } public double getPrice() { return price; } public void setPrice(double pri) { price = pri; } }
public class TestPizza { public static void main(String[] args) { Pizza pie = new Pizza(); pie.setPrice(14); pie.setToppings("Mushrooms"); pie.setDiameter(20); System.out.println("Diameter is: " + pie.getDiameter()); System.out.println("Toppings are: " + pie.getToppings()); System.out.println("Price is: $" + pie.getPrice()); } }
public class Student { private int id; private int numberOfCreditHours; private int numberOfPointsEarned; private double gPA; public Student() { id = 9999; numberOfCreditHours = 3; numberOfPointsEarned = 12; } public int getId() { return id; } public void setId(int identificationNumber) { id = identificationNumber; } public int getNumberOfCreditHours() { return numberOfCreditHours; } public void setNumberOfCreditHours(int creditHours) { numberOfCreditHours = creditHours; } public int getNumberOfPointsEarned() { return numberOfPointsEarned; } public void setNumberOfPointsEarned(int pointsEarned) { numberOfPointsEarned = pointsEarned; } public double calculateGPA() { return (numberOfPointsEarned / numberOfCreditHours); } }
public class ShowStudent { public static void main(String[] args) { Student a = new Student(); a.setId(123); a.setNumberOfCreditHours(3); a.setNumberOfPointsEarned(12); System.out.println("ID is: " + a.getId()); System.out.println("Credit hours: " + a.getNumberOfCreditHours()); System.out.println("Points earned: " + a.getNumberOfPointsEarned()); System.out.println("GPA is: " + a.calculateGPA()); } }
public class ShowStudent2 { public static void main(String[] args) { Student a = new Student(); System.out.println("ID is: " + a.getId()); System.out.println("Credit hours: " + a.getNumberOfCreditHours()); System.out.println("Points earned: " + a.getNumberOfPointsEarned()); System.out.println("GPA is: " + a.calculateGPA()); } }
public class Checkup { private int patientNumber; private int bPS; private int bPD; private double lDL; private double hDL; public void setPatientNumber(int num) { patientNumber = num; } public int getPatientNumber() { return patientNumber; } public void setBPS(int num) { bPS = num; } public int getBPS() { return bPS; } public void setBPD(int num) { bPD = num; } public int getBPD() { return bPD; } public void setLDL(double num) { lDL = num; } public double getLDL() { return lDL; } public void setHDL(double num) { hDL = num; } public double getHDL() { return hDL; } public void computeRatio() { System.out.println("The result is: " + (lDL/hDL)); } public static void explainRatio() { System.out.println("HDL is known as the good cholesterol"); System.out.println("A ratio of 3.5 or lower is considered optimum"); } }
import java.util.Scanner; public class TestCheckup { public static void main(String[] args) { Checkup firstPatient = new Checkup(); Checkup secondPatient = new Checkup(); Checkup thirdPatient = new Checkup(); Checkup fourthPatient = new Checkup(); firstPatient = getValues(firstPatient); showValues(firstPatient); secondPatient = getValues(secondPatient); showValues(secondPatient); thirdPatient = getValues(thirdPatient); showValues(thirdPatient); fourthPatient = getValues(fourthPatient); showValues(fourthPatient); } public static Checkup getValues(Checkup patient) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter the patient number: "); patient.setPatientNumber(keyboard.nextInt()); System.out.print("Enter the patient systolic BP: "); patient.setBPS(keyboard.nextInt()); System.out.print("Enter the patient diastolic BP: "); patient.setBPD(keyboard.nextInt()); System.out.print("Enter the patient LDL: "); patient.setLDL(keyboard.nextDouble()); System.out.print("Enter the patient HDL: "); patient.setHDL(keyboard.nextDouble()); return patient; } public static void showValues(Checkup patient) { System.out.println("The patient number is: " + patient.getPatientNumber()); System.out.println("BP: " + patient.getBPS() + "/" + patient.getBPD()); patient.computeRatio(); patient.explainRatio(); } }
public class Invoice { private int itemNumber; private String name; private int quantity; private double price; private double totalCost; public void setItemNumber(int num) { itemNumber = num; } public int getItemNumber() { return itemNumber; } public void setName(String nam) { name = nam; } public String getName() { return name; } public void setQuantity(int num) { quantity = num; recalculateTotalCost(); } public int getQuantity() { return quantity; } public void setPrice(double num) { price = num; recalculateTotalCost(); } public double getPrice() { return price; } public void recalculateTotalCost() { totalCost = (price * quantity); } public double getTotalCost() { return totalCost; } public void displayLine() { System.out.println("Item number: " + itemNumber); System.out.println("Name: " + name); System.out.println("Quantity: " + quantity); System.out.println("Price: " + price); System.out.println("Total cost: " + totalCost); } }
import java.util.Scanner; public class TestInvoice { public static void main(String[] args) { Invoice invoice = new Invoice(); invoice = getValues(invoice); invoice.displayLine(); } public static Invoice getValues(Invoice inv) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter item number: "); inv.setItemNumber(keyboard.nextInt()); keyboard.nextLine(); System.out.print("Enter item name: "); inv.setName(keyboard.nextLine()); System.out.print("Enter quantity: "); inv.setQuantity(keyboard.nextInt()); System.out.print("Enter the price: "); inv.setPrice(keyboard.nextDouble()); return inv; } }
public class Card { private char suit; private int val; public void setSuit(char s) { suit = s; } public char getSuit() { return suit; } public void setVal(int v) { val = v; } public int getVal() { return val; } }
import java.util.Random; public class PickTwoCards { public static void main(String[] args) { Cards firstCard = new Cards(); firstCard = getCard(firstCard); Cards secondCard = new Cards(); secondCard = getCard(secondCard); firstCard.setSuit('s'); secondCard.setSuit('h'); System.out.println("The first card"); showCard(firstCard); System.out.println("The second card"); showCard(secondCard); } public static Cards getCard(Cards aCard) { final int CARDS_IN_SUIT = 13; aCard.setVal(((int)(Math.random() * 100) % CARDS_IN_SUIT + 1)); return aCard; } public static void showCard(Cards aCard) { System.out.println("suit: " + aCard.getSuit()); System.out.println("value: " + aCard.getVal()); } }
public class MyCharacter { private String color; private int eyes; private int lives; public String getColor() { return color; } public void setColor(String col) { color = col; } public int getEyes() { return eyes; } public void setEyes(int eye) { eyes = eye; } public int getLives() { return lives; } public void setLives(int life) { lives = life; } }
import java.util.Scanner; public class TwoCharacters { public static void main(String[] args) { MyCharacter first = new MyCharacter(); System.out.println("First character"); first = design(); MyCharacter second = new MyCharacter(); System.out.println("Second character"); second = design(); System.out.println("First character"); displayCharacters(first); System.out.println("Second character"); displayCharacters(second); } public static MyCharacter design() { MyCharacter tempCharacter = new MyCharacter(); Scanner keyboard = new Scanner(System.in); System.out.print("Enter color: "); tempCharacter.setColor(keyboard.nextLine()); System.out.print("Enter number of eyes: "); tempCharacter.setEyes(keyboard.nextInt()); System.out.print("Enter number of lives: "); tempCharacter.setLives(keyboard.nextInt()); return tempCharacter; } public static void displayCharacters(MyCharacter character) { System.out.println("Color of character: " + character.getColor()); System.out.println("Number of eyes: " + character.getEyes()); System.out.println("Number of lives: " + character.getLives()); } }