Cont'd from Lesson 2: PPT
7. Clue 4: How to use main method in a class? Examples of classes
Elaboration: (PPT-L2 Slides 24-28), main() method and a Test class
Prompts & Check : Dance with a class now - Kahoot! 1-7
8. Clue 5: How to declare object variables and create objects from a class? Difference between primitive types and object references.
Elaboration: (PPT-L2 Slides 30-36)
Prompts & Check : Create some objects of a class now - Slide #37
9. Clue 6: How to use a constructor? How to access object instance variables and methods?
Elaboration: (PPT-L2 Slides 30-38)
Prompts & Check : Create constructors, use them to create more objects and access instance variables and methods of those objects - Slide #39
10. Clue 7: Instance variables vs static variables, how about methods?
Elaboration: (PPT-L2 Slides 40-43)
Prompts & Check : Kahoot! 9-26
11. Clue 8: Passing objects as parameters of methods - pass by reference.
Elaboration: (PPT-L2 Slides 44-45)
Challenges:
1. Turn ACSL Scrabble into a class with the following design:
1) Add an instance variable to store the initial string. Create a constructor with a string parameter to initialize this instance variable
2) Add an instance method to calculate scores of the string object. The method should take an integer parameter to specify beginning position where the string will be put at.
Create an object in main() by calling the constructor. Run the score method at different starting positions and print out the score.
2. Turn ACSL Map into a class "Trip" and a class "Vehicle" with the following design:
1) )Add "milesPerHour", "averageSpeed" as two instance variables in class Vehicle.
2) Add "startCity", "endCity" and "gasPriceToday" as instance variables in class Trip.
3)Add a constructor of class Trip to initialize all instance variables
4)Add instance methods to return all necessary values to complete the task as required.
3. (Coding Practice): ACSL_DraftPick
1.
import java.io.IOException;
import java.io.FileReader;
import java.util.*;
class Word {
String input;
public int getPoints(int position){
int points = 0;
int multiplier = 1;
for(int j=0; j<4; j++){
int lettervalue = 0;
double letterpos = position + j;
switch (input.charAt(j)+""){
case "A": lettervalue = 1; break;
case "E": lettervalue = 1; break;
case "D": lettervalue = 2; break;
case "R": lettervalue = 2; break;
case "B": lettervalue = 3; break;
case "M": lettervalue = 3; break;
case "V": lettervalue = 4; break;
case "Y": lettervalue = 4; break;
case "J": lettervalue = 8; break;
case "X": lettervalue = 8; break;
default: break;
}
if(letterpos%3==0 && letterpos%2!=0){
lettervalue = lettervalue*2;
}
else if(letterpos%5==0){
lettervalue = lettervalue*3;
}
else if(letterpos%7==0 && multiplier==1){
multiplier = 2;
}
else if(letterpos%8==0 && multiplier==1){
multiplier = 3;
}
points = points + lettervalue;
}
return points*multiplier;
}
Word(String input){
this.input = input;
}
public static void main(String[] args) throws IOException {
Scanner Howard = new Scanner(new FileReader("data/input.txt"));
Word obj1 = new Word(Howard.nextLine());
for(int i=0; i<5; i++){
System.out.println(obj1.getPoints(Howard.nextInt()));
}
}
}
2.
import java.io.IOException;
import java.io.FileReader;
import java.util.*;
class Vehicle{
int milesPerGallon;
int averageSpeed;
public String getTimeTravelled(int distance){
int hours = distance/averageSpeed;
int minutes = (int)Math.round((distance%averageSpeed)/((double)averageSpeed)*60);
String returnHours = "" + hours;
String returnMinutes = "" + minutes;
if(hours<9){returnHours = "0" + hours;}
if(minutes<9){returnMinutes = "0" + minutes;}
return returnHours + ":" + returnMinutes;
}
public String getGasPrice(double prices, int distance){
double roundPrice = Math.round(prices*100.0*distance/milesPerGallon)/100.0;
String finalPrice = "$" + roundPrice;
if(((roundPrice*10)%1)/10 == 0){finalPrice = finalPrice + "0";}
return finalPrice;
}
Vehicle(int milesPerGallon, int averageSpeed){
this.milesPerGallon = milesPerGallon;
this.averageSpeed = averageSpeed;
}
}
class Trip{
String[] startAndEndCity = new String[2];
double gasPriceToday;
int[] distances = {450,140,120,320,250,80};
public int getDistance(){
int[] endpoints = new int[2];
for(int i=0; i<2; i++){
switch(startAndEndCity[i]){
case "A": endpoints[i] = 0; break;
case "B": endpoints[i] = 1; break;
case "C": endpoints[i] = 2; break;
case "D": endpoints[i] = 3; break;
case "E": endpoints[i] = 4; break;
case "F": endpoints[i] = 5; break;
case "G": endpoints[i] = 6; break;
default: break;
}
}
int totaldistance = 0;
for(int i=endpoints[0]; i<endpoints[1]; i++){
totaldistance = totaldistance + distances[i];
}
return totaldistance;
}
Trip(String startCity, String endCity, double gasPriceToday){
startAndEndCity[0] = startCity;
startAndEndCity[1] = endCity;
this.gasPriceToday = gasPriceToday;
}
}
class RouteStats {
public static void main(String[] args) throws IOException {
Scanner Meredith = new Scanner(new FileReader("data/input.txt"));
String[] input = Meredith.nextLine().split(", ");
Vehicle Car = new Vehicle(Integer.parseInt(input[0]), Integer.parseInt(input[2]));
for(int i=0; i<5; i++){
String[] endpoints = Meredith.nextLine().split(", ");
Trip Vacation = new Trip(endpoints[0], endpoints[1], Double.parseDouble(input[1]));
System.out.print(Vacation.getDistance() + ", ");
System.out.print(Car.getTimeTravelled(Vacation.getDistance()) + ", ");
System.out.println(Car.getGasPrice(Vacation.gasPriceToday, Vacation.getDistance()));
}
}
}