java 102 intro to object-oriented programming in java - exercises
TRANSCRIPT
![Page 1: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/1.jpg)
Java 102: Intro to Object-oriented Programming in Java
Hands-on Exercises
![Page 2: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/2.jpg)
Hands-on Exercise
Creating Objects
![Page 3: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/3.jpg)
Exercise: Creating Objects
• Create a new Java project named Java102• Create a new package named exercise.carfactory• Create a class named Car in the exercise.carfactory
package• Add color, make and model properties to the Car class• Create a java program named CarFactory (in same
package) that creates two instances of the class Car, changes their colors to Blue and Pink and prints a message to the console
• Run the class CarFactory and observe the message in the Console.
![Page 4: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/4.jpg)
Solution: Creating Objects
package exercise.creatingobjects;
public class Car {String make;String model;String color;
}
package exercise.creatingobjects;
public class CarFactory {
public static void main(String[] args) {Car firstCar = new Car();Car secondCar = new Car();
firstCar.color = "Blue";secondCar.color = "Pink";
System.out.println("Just finished painting new cars");}
}
Car.java
CarFactory.java
![Page 5: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/5.jpg)
Hands-on Exercise
Working with Methods
![Page 6: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/6.jpg)
Exercise: Working with Methods
• What happens when you compile and run the following code?
public class Cubes {
static int cube (int i){int j = i * i * i;return j;
}
public static void main(String[] args) {int N = Integer.parseInt(args[0]);for (int i=0 ;i<= N; i++) {
System.out.println(i + " " + cube(i));}
}}
![Page 7: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/7.jpg)
Solution: Working with Methodspublic class Cubes {
static int cube (int i){
int j = i * i * i;
return j;
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
for (int i=0 ;i<= N; i++) {
System.out.println(i + " " + cube(i));
}
}
}% java Cubes 6
0 0
1 1
2 8
3 27
4 64
5 125
6 216
![Page 8: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/8.jpg)
Hands-on Exercise
Method Overloading
![Page 9: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/9.jpg)
Exercise : Method Overloading
• Create a new package named exercise.methodoverloading• Create a BasicRateTax class with a method calcTax() that returns 20% of a fixed
base income of £1000• Create a java program named TaxCollector that creates a new BasicRateTax object,
calls the calcTax() method and prints the output to the console• Run the TaxCollector program and ensure it always prints 200.00 as calculated tax• Add new calcTax() method to BasicRateTax class that takes a double grossIncome
parameter and calculates the tax as 20% of the grossIncome if it’s greater than the base income of £1000
• Change the TaxCollector program to call the new calcTax(double grossIncome) method and passing the gross Income value from the command line
• Run the TaxCollector program and see if the tax is correctly calculated. • Re-run the program with different Gross Income values and check the output
![Page 10: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/10.jpg)
Solution: Method Overloadingpackage exercise.methodoverloading;
public class BasicRateTax {private static final double BASE_INCOME = 1000.00;
private static final double BASIC_TAX_RATE = 0.20;
public double calcTax (){return BASE_INCOME * BASIC_TAX_RATE;
}
public double calcTax(double grossIncome){if (grossIncome < BASE_INCOME){
return calcTax();}return grossIncome * BASIC_TAX_RATE;
}}
![Page 11: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/11.jpg)
Solution: Method Overloading
package exercise.methodoverloading;
public class TaxCollector {
public static void main(String[] args) {double grossIncome = Double.parseDouble(args[0]);
BasicRateTax taxCalculator = new BasicRateTax();
double tax = taxCalculator.calcTax(grossIncome);
System.out.println("Tax due is " + tax);}
}% java TaxCollector 2000
Tax due is 400.0
% java TaxCollector 10000
Tax due is 2000.0
![Page 12: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/12.jpg)
Hands-on Exercise
Inheritance
![Page 13: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/13.jpg)
Exercise: Inheritance
• Create a new package named exercise.inheritance• Create a class named HigherRateTax in the exercise.inheritance package that
extends BasicRateTax and add an empty calcTax(double grossIncome) method • Add the code to HigherRateTax.calcTax(double grossIncome) method to calculate
the tax as follows:– 20% of grossIncome if up to £34,000 (hint: reuse the BasicRateTax.calcTax(double
grossIncom) method)– 40% of grossIncome if above £34,000 but less than £150,000– 50% of grossIncome if £150,000 or above
• Run the existing TaxCollector program with some large gross income amounts and observe that your changes didn’t have any effect on the calculate tax. Why?
• Change the code of the TaxCollector to instantiate HigherRateTax instead of BasicRateTax
• Run the TaxCollector program again and observe that now the new percentage is properly applied. You are now using the overridden version of the method calcTax().
![Page 14: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/14.jpg)
Solution: Inheritancepackage exercise.inheritance;
import exercise.methodoverloading.BasicRateTax;
public class HigherRateTax extends BasicRateTax {
public double calcTax(double grossIncome){double tax = 0.0;if (grossIncome <=34000.00){
tax = super.calcTax(grossIncome);}else if (grossIncome > 34000 && grossIncome <=150000) {
tax = grossIncome * 0.40;}else if (grossIncome > 150000){
tax = grossIncome * 0.50;}return tax;
}}
![Page 15: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/15.jpg)
Solution: Inheritancepackage exercise.methodoverloading;
import exercise.inheritance.HigherRateTax;
public class TaxCollector {
public static void main(String[] args) {double grossIncome = Double.parseDouble(args[0]);
BasicRateTax taxCalculator = new HigherRateTax ();
double tax = taxCalculator.calcTax(grossIncome);
System.out.println("Tax due is " + tax);}
}% java TaxCollector 51000
Tax due is 20400.0
% java TaxCollector 32000
Tax due is 6400.0
% java TaxCollector 155000
Tax due is 77500.0
![Page 16: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/16.jpg)
Hands-on Exercise
Using a Java Library
![Page 17: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/17.jpg)
Exercise: Using Libraries
• Create a new package named exercise.libraryclient
• Create a class named CardDealer with an empty deal() method that takes no arguments and returns a String
• Implement the card dealer to use the StdRandomlibrary to deal playing cards ramdomly from an infinite deck of cards
• Create a CardDealerTest program to test the CardDealer class
![Page 18: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/18.jpg)
Exercise: Using the StdRandom Library
public class CardDealer {
private static final String[] SUITES = { "D", "H", "C", "S"
};
private static final int TOTAL_CARDS_PER_SUITE = 13;
public String deal() {
// select a random suite
String suite = SUITES[StdRandom.uniform(SUITES.length)];
// select a random rank
int rank = StdRandom.uniform (TOTAL_CARDS_PER_SUITE ) +
1;
String card = rank + suite;
// return the dealt card
return card;
}
}
![Page 19: Java 102 intro to object-oriented programming in java - exercises](https://reader031.vdocument.in/reader031/viewer/2022012402/55a667cf1a28abbf668b45b3/html5/thumbnails/19.jpg)
Testing the CardDealer Program
public class CardDealerTest {
public static void main(String[] args) {
CardDealer dealer = new CardDealer();
for (int i=0;i<5;i++){
String card = dealer.deal();
System.out.println( “ Card “ + i + “ is “ + card);
}
}
}