1. PPT: https://drive.google.com/open?id=1zrzHiiwS3X8gs05N2IuaNacj3U6zw8PD
2. Flashback:
case 1: Review the frequently used methods in the following classes: String, Integer, Scanner
case 2: ACSL Scrabble (answer below)The corresponding code is demoed in Codiva. 3. Prompts: in reviewing the concept of OOP, classes and objects, you might have the following questions.
1). How do we define a class?
2). How to create objects?
3). How to use classes and object to write code? 4. Clue 1: Classes declarations: How to add attributes
Elaboration: (PPT-L2 Slides 1-7)
Prompts & Check : (Slide #8, create classes in Codiva and adding instance variables). 5. Clue 2 : Classes declarations: How to add methods?
Elaboration: (PPT-L2 Slides 9-15)
Prompts & Check : Adding methods to your classes (Slide #16). 6. Clue 3: Passing parameters to a method
Elaboration: (PPT-L2 Slides 17-22)
Prompts & Check : Create methods with parameters passing (Slide #23).
7 Challenges:
Finish up all the classes you created so far: Circle, Person, Automobile, create 2+ objects of each class. Explain steps to access instance variables and methods of these objects.
(Coding Practice): ACSL_Map
1.
class Person {
int age;
String name;
double height;
String gender;
String nationality;
public void changeName(String newName){
name = newName;
}
public void waitYears(int numberOfYears){
age += numberOfYears;
}
public void grow(double growth){
height += growth;
}
public void changeCitizenship(String newCountry){
nationality = newCountry;
}
public static void main(String[] args) {
Person MarkWatney = new Person();
MarkWatney.nationality = "American";
MarkWatney.changeCitizenship("Martian");
System.out.println(MarkWatney.nationality);
Person Dwarf = new Person();
Dwarf.name = "Shorty";
Dwarf.height = 100;
Dwarf.age = 12;
Dwarf.waitYears(5);
Dwarf.grow(100);
Dwarf.changeName("Giant");
System.out.println(Dwarf.name + " is now " + Dwarf.height + " cm tall and " + Dwarf.age + " years old.");
}
}
class Circle {
double radius;
public double findArea(){
return radius*radius*Math.PI;
}
public double findCircumfrence(){
return radius*2*Math.PI;
}
public void scale(double scalar){
radius = radius*scalar;
}
public static void main(String[] args) {
Circle circleX = new Circle();
circleX.radius = 67;
System.out.println(circleX.findArea());
Circle roundBoi = new Circle();
roundBoi.radius = 0.5/Math.PI;
System.out.println(roundBoi.findCircumfrence());
roundBoi.scale(6);
System.out.println(roundBoi.findCircumfrence());
}
}
class Car {
String colour;
double speed;
double timeDriving;
String licensePlate;
double fuelEfficiency;
double maxFuel;
double currentFuel;
public void accelerate(double acceleration){
if(maxFuel>=((speed + acceleration)*timeDriving/fuelEfficiency)){
speed = speed + acceleration;
currentFuel = maxFuel-(speed*timeDriving/fuelEfficiency);
}
else{
speed = 0;
currentFuel = 0;
}
}
public void fillTank(){
currentFuel = maxFuel;
}
public void paintCar(String newColour){
colour = newColour;
}
public Car(){
speed = 0;
timeDriving = 0;
}
public static void main(String[] args) {
Car Ford = new Car();
Ford.colour = "Red";
Ford.paintCar("Green");
System.out.println(Ford.colour);
Car USSEnterprise = new Car();
USSEnterprise.maxFuel = 999999999;
USSEnterprise.fillTank();
USSEnterprise.fuelEfficiency = 100;
USSEnterprise.timeDriving = 48;
USSEnterprise.accelerate(300000000);
System.out.println(USSEnterprise.currentFuel);
}
}
2.
import java.util.*;
class Driving {
public static void main(String[] args) {
Scanner Meredith = new Scanner(System.in);
String input = Meredith.nextLine();
String[] parts = input.split(", ");
double gallon = Double.parseDouble(parts[0]);
double price = Double.parseDouble(parts[1]);
double speed = Double.parseDouble(parts[2]);
int[] distances = {450,140,120,320,250,80};
for(int i=0; i<5; i++){
String route = Meredith.nextLine();
String[] components = route.split(", ");
int[] endpoints = new int[2];
for(int j=0; j<2; j++){
switch(components[j]){
case "A": endpoints[j] = 0; break;
case "B": endpoints[j] = 1; break;
case "C": endpoints[j] = 2; break;
case "D": endpoints[j] = 3; break;
case "E": endpoints[j] = 4; break;
case "F": endpoints[j] = 5; break;
case "G": endpoints[j] = 6; break;
default: break;
}
}
double totaldistance = 0;
for(int j=endpoints[0]; j<endpoints[1]; j++){
totaldistance = totaldistance + distances[j];
}
System.out.print(totaldistance + ", ");
int hours = (int)(totaldistance/speed);
int minutes = (int)Math.round((totaldistance%speed)/speed*60);
if(hours>9){System.out.print(hours + ":");}
else{System.out.print("0" + hours + ":");}
if(minutes>9){System.out.print(minutes + ", ");}
else{System.out.print("0" + minutes + ", ");}
double totalprice = Math.round((totaldistance/gallon*price)*100)/100.0;
System.out.print("$" + totalprice);
if(((totalprice*10)%1)/10 == 0){System.out.println(0);}
else{System.out.println();}
}
}
}