CSCI 282 Lab 9 Purpose: Get more practice with Interfaces, abstract classes, fin
ID: 3815758 • Letter: C
Question
CSCI 282 Lab 9 Purpose: Get more practice with Interfaces, abstract classes, final methods, final classes, overriding toString0 method, and polymorphism. Directions Use Eclipse Download the files and add the code described in the comments Submission: Create a new folder Rename it to your first and last name (e.g. KevinCherry) Put all submitted content into that folder Zip up the folder Submit the zipped folder on Moodle Penalities: I will take off points for the following things: o Not following submission directions o Not following lab directions o Submitting .class files o Not having visibility modifiers on all fields, classes and methods o Having your program crash or hang while executing o Having code that doesn't compile or run (automatic zero for the lab) o Not properly indenting your code Also mixing tabs and spaces Either ONLY use leading spaces before each line of code OR use ONLYleading tabs before each line of code. Do not have one line of code using leading tabs and another using leading spaces. It may look fine in your editor BUT MAY NOTlook fine in other people's editors. o Having too many blank lines Some blank lines help readability of code but too many just looks messy and actualy makes code less readable Always think about someone else reading your code possible, but without making it My rule of thumb is to put as much on one screen as look cluttered. sparingly, only enough to enhance readability but In other words, just newlines no more. o Not having meaningful variable, method or class names something descriptive If I tell you what to name it, use name, otherwise use thatExplanation / Answer
public class Main
{
public static void main (String[] args)
{
Vehicle SC = new SportsCar();
Vehicle FC = new FamilyCar();
SC.goFaster();
System.out.println(SC.toString());
SC.stop();
System.out.println(SC.toString());
FC.goFaster();
FC.goFaster();
System.out.println(FC.toString());
FC.stop();
System.out.println(FC.toString());
}
}
public final class Prius extends Car implements Vehicle
{
public final boolean hasCupHolders()
{
return true;
}
public void goFaster()
{
speed = speed + 0.5;
}
public void goSlower()
{
speed = speed - 0.5;
}
public final void stop()
{
super.stop();
}
}
public final class SportsCar extends Car implements Vehicle
{
public boolean hasCupHolders()
{
return false;
}
public void goFaster()
{
speed = speed + 2;
}
public void goSlower()
{
speed = speed - 2;
}
public final void stop()
{
super.stop();
}
}
public class FamilyCarSlowerModel extends FamilyCar
{
public void goFaster()
{
speed = speed + 1;
}
public void goSlower()
{
speed = speed - 2;
}
}
public class FamilyCar extends Car implements Vehicle
{
public boolean hasCupHolders()
{
return true;
}
public void goFaster()
{
speed = speed + 1.5;
}
public void goSlower()
{
speed = speed - 1.5;
}
public final void stop()
{
super.stop();
}
}
public abstract class Car
{
public double speed;
public abstract boolean hasCupHolders();
public void stop()
{
speed = 0;
}
public String toString()
{
return "This is "+getClass().getName()+ ". It has cup holders : "+hasCupHolders()+" and the speed of the car is "+speed ;
}
}
public interface Vehicle
{
void goFaster();
void goSlower();
void stop();
}
Output:
This is SportsCar. It has cup holders : false and the speed of the car is 2.0
This is SportsCar. It has cup holders : false and the speed of the car is 0.0
This is FamilyCar. It has cup holders : true and the speed of the car is 3.0
This is FamilyCar. It has cup holders : true and the speed of the car is 0.0