Question

Project 7: Vehicles 1 Objective In the last couple projects, you’ve created and used objects in...

Project 7: Vehicles

1 Objective

In the last couple projects, you’ve created and used objects in interesting ways. Now you’ll get a chance to use more of what objects offer, implementing inheritance and polymorphism and seeing them in action. You’ll also get a chance to create and use abstract classes (and, perhaps, methods). After this project, you will have gotten a good survey of object-oriented programming and its potential. This project won’t have a complete UI but will have a main program running the show and giving you an opportunity to verify your results. Note that this project does not use Interfaces; we’ll rely on inheritance instead.

2 Vehicles

Create a model that supports the variety of vehicles shown below1. For all vehicles we’ll want to know many people it can carry (aka the occupant count).

  • Powered – we’re interested in knowing the power source (either gas, electric, hydrogen, or hybrid), the horsepower, the transmission type (either single speed, automatic, manual, or CVT), and how many wheels that vehicle has.

    • Automobile – these should default to four wheels.

      • Car – we’re interested in the style (sedan or coupe), plus whether there’s a hatchback.

      • Truck – we’ll want to know the towing capacity (in pounds, an integer).

    • Motorcycle – these always have two wheels.

    • Hoverboard – these are always electric, and only support one occupant.

  • Manual – power sources for these include human, horse, and gravity.

• Rolling – we’re interested in how many wheels are on these vehicles.

• Cycle – these are human powered, support one occupant, and have two wheels by default. • Bicycle – these are a typical type of cycle.

• Tandem Bicycle – unusual in the cycle category, these have three wheels and support two occupants.

  • Unicycle – these have one wheel, have only one speed, and don’t offer power assist.

  • Tricycle – these have three wheels.

  • Skateboard – these are human powered, support one occupant, and have four wheels.

  • Roller Skates – these are human powered and support one occupant. Wheel count varies.

  • Carriage – these are horse powered.

• Sliding – we want to keep track of how many surfaces these vehicles use.

  • Ice Skates – these are human powered, support one occupant, and have two surfaces.

  • Skis – these are gravity-powered, support one occupant, and have two surfaces.

  • Snowboard – these are gravity powered, support one occupant, and have one surface.

  • Sleigh – these are horse powered and have two surfaces.

    1 I know that you’ll read some of these and want to argue about the classifications. But we must simplify the world a bit in order to learn what we need to learn and to keep this from exploding into a hundred classes.

Page 1 of 4

Java I Barry

3 toString

While often toString methods are minimal and bare, in this case we’ll use them to help demonstrate the power of inheritance and polymorphism. These should show complete information about the vehicle that was instantiated. For example, here’s my output for one sample car instance:

Car
Occupant count: 4
Power Source: GAS
Trans Type: AUTOMATIC
Horsepower: 240
Wheel Count: 4
Style: SEDAN
Has Hatchback? false

4 Code Implementation

4.1 Classes

You’ll create quite a few class files, each in its own .java file. You will likely also want at least one enumerated type; these should also be created within the appropriate classes for better encapsulation.

Carefully consider which classes and methods should be declared as abstract, and which are concrete. Carefully consider where to declare methods, moving them up to the topmost pertinent class. Override methods in lower classes when necessary (and calling methods from the superclass when appropriate).

4.2 Main

Create an additional Main class containing a main program. In it instantiate each type of concrete object, adding each to an array of top-most superclass references. Write code to loop through all array items, displaying each object on the screen (implicitly calling its toString method).

4.3 Constructors and Accessors

Provide a full constructor for each class, specifying all relevant data for instantiated objects. Remember that even abstract classes can (and should, often) have constructors. Don’t forget that the first call a constructor must make is to call the constructor of its superclass. Provide standard accessors for pertinent properties; don’t worry about mutators, however.

4.4 Constants

Create and use constants where they make sense. Make sure they are marked correctly, e.g., final static.

4.5 Style

Follow the provided Course Style Guide.

(continues...)

Page 2 of 4

Java I Barry

5 Notes

Make sure constructors ask for only the required information, e.g., for a Unicycle it wouldn’t make sense for there to be any parameters.
Push all work up the hierarchy as far as possible.
Don’t duplicate code where it’s avoidable; if you find yourself doing the same exact thing in class after class, think about ways to avoid that.

Use polymorphism to your advantage; some classes will appear nearly empty (with only a constructor and perhaps a couple of constants).
Implementing this model should require no loops and no selection control structures. Main’s code will involve looping, however; that’s fine.

6 UML

Organize your classes within the BlueJ class browser; place the topmost superclass at the top and subclasses under it, making the hierarchy clear. Place the Main class at the top or far left so it’s easy to find. The diagram should look clean and easy to read.

7 Extra Credit: Testing

Write JUnit tests for each concrete class. While you can’t directly test abstract classes, you can test their code by testing one of their children more thoroughly. Otherwise, focus on what is new to each class. Make sure the expected attributes are in place, e.g., that a Unicycle has one wheel, one speed, and no power assist. This will also help you review your constructors to make sure they are minimal.

9 Hints

  • Start by sketching out a basic UML Class Diagram showing the classes and relationships in a hierarchy. Fill in private data and methods so you can get clear on where things go. If you have struggles, I’ll want to see this before when we discuss your issues.

  • You may be tempted to pull literals, constants, or computations out of the individual classes; don’t do it. We want them there for purposes of demonstrating OOP concepts. Plus, remember that objects are encapsulated; they should have in them all data and code that pertains to them.

  • BlueJ’s class browser is your friend; it will draw a simplified UML Class Diagram for you as you create subclasses and will also denote abstract classes where you’ve used them. Make sure it makes sense as you go along.

  • The class browser offers a button that compiles the entire project; that’s useful when you make changes to a superclass.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
package HomeworkLib.poly;

import java.util.List;

public class VechileMain {
    public static void main(String[] args){
        Vechile[] stock =new Vechile[10];

        stock[0] =new Car("SEDAN", true);
        stock[1] =new Bicycle();
        stock[2] =new Truck(1000);
        stock[3] =new Sleigh();
        stock[4] =new IceStake();
        stock[5] =new Motorcycle();
        stock[6] =new Carriage();
        stock[7] =new Unicycle();
        stock[8] =new Snowboard();
        stock[9] =new Rolling(2);


        for(int i=0;i<stock.length;i++){
            System.out.println(stock[i].toString());
        }

    }
}

package HomeworkLib.poly;

public enum TransmissionType {
SINGLE_SPEED,
AUTOMATIC,
MANUAL,
CVT
}

package HomeworkLib.poly;

public abstract class Vechile {
protected int occupantCount;
protected PowerSource powerSource;
protected TransmissionType transmissionType;
protected double hoursePower;
protected int wheelCount;

@Override
public String toString() {
return "Vechile{" +
"occupantCount=" + occupantCount +
", powerSource=" + powerSource +
", transmissionType=" + transmissionType +
", hoursePower=" + hoursePower +
", wheelCount=" + wheelCount +
'}';
}
}

package HomeworkLib.poly;

public class Automobile extends Vechile {

public Automobile() {
this.wheelCount =4;
this.transmissionType = TransmissionType.AUTOMATIC;
}


}

package HomeworkLib.poly;

public class Car extends Automobile {

private String style;
private boolean isHatchback;

public Car(String style, boolean isHatchback) {
this.style = style;
this.isHatchback = isHatchback;
this.occupantCount =4;
this.powerSource=PowerSource.GAS;
this.hoursePower =240;
}

public Car(int occupantCount, PowerSource powerSource, double hoursePower, String style, boolean isHatchback){
super();
this.style =style;
this.occupantCount =occupantCount;
this.powerSource=powerSource;
this.hoursePower =hoursePower;
this.isHatchback =isHatchback;
}


@Override
public String toString() {
return "Car{" +
"style='" + style + '\'' +
", isHatchback=" + isHatchback +
", occupantCount=" + occupantCount +
", powerSource=" + powerSource +
", transmissionType=" + transmissionType +
", hoursePower=" + hoursePower +
", wheelCount=" + wheelCount +
"} " + super.toString();
}
}

package HomeworkLib.poly;

public class Truck extends Automobile {
protected int towingCapacity;

public Truck(int towingCapacity) {
super();
this.towingCapacity = towingCapacity;
}

@Override
public String toString() {
return "Truck{" +
"towingCapacity=" + towingCapacity +
"} " + super.toString();
}
}

package HomeworkLib.poly;

public class Motorcycle extends Automobile {
public Motorcycle() {
super();
this.wheelCount =2;
}

@Override
public String toString() {
return "Motorcycle{} " + super.toString();
}
}

package HomeworkLib.poly;

public class Hoverboard extends Automobile {

public Hoverboard() {
super();
this.occupantCount =1;
this.powerSource =PowerSource.ELECTRIC;
}

@Override
public String toString() {
return "Hoverboard{" +
"occupantCount=" + occupantCount +
", powerSource=" + powerSource +
", transmissionType=" + transmissionType +
", hoursePower=" + hoursePower +
", wheelCount=" + wheelCount +
"} " + super.toString();
}
}

package HomeworkLib.poly;

public enum PowerSource {
GAS,
ELECTRIC,
HYDROGEN,
HYBRID,
HUMAN,
HORSE,
GRAVITY
}

package HomeworkLib.poly;

public class Rolling extends Manual{
public Rolling(int wheelCount) {
super();
this.wheelCount =wheelCount;
}

@Override
public String toString() {
return "Rolling{" +
"occupantCount=" + occupantCount +
", powerSource=" + powerSource +
", transmissionType=" + transmissionType +
", hoursePower=" + hoursePower +
", wheelCount=" + wheelCount +
"} " + super.toString();
}
}

package HomeworkLib.poly;

public class Cycle extends Manual{
public Cycle() {
super();
this.powerSource=PowerSource.HUMAN;
this.occupantCount=1;
this.wheelCount=2;
}

@Override
public String toString() {
return "Cycle{" +
"occupantCount=" + occupantCount +
", powerSource=" + powerSource +
", transmissionType=" + transmissionType +
", hoursePower=" + hoursePower +
", wheelCount=" + wheelCount +
"} " + super.toString();
}
}

package HomeworkLib.poly;

public class Bicycle extends Cycle {
public Bicycle() {
super();
}
}

package HomeworkLib.poly;

public class Unicycle extends Cycle {

public Unicycle() {
super();
this.wheelCount =1;
this.transmissionType =TransmissionType.SINGLE_SPEED;
this.powerSource =null;
}
}

package HomeworkLib.poly;

public class Tricycle extends Cycle{
public Tricycle() {
super();
this.wheelCount=3;
}
}

package HomeworkLib.poly;

public class Skateboard extends Manual{
public Skateboard() {
super();
this.powerSource=PowerSource.HUMAN;
this.occupantCount=1;
this.wheelCount=4;
}
}

package HomeworkLib.poly;

public class Manual extends Vechile{

public Manual() {
super();
this.transmissionType =TransmissionType.MANUAL;
}

@Override
public String toString() {
return "Manual{" +
"occupantCount=" + occupantCount +
", powerSource=" + powerSource +
", transmissionType=" + transmissionType +
", hoursePower=" + hoursePower +
", wheelCount=" + wheelCount +
"} " + super.toString();
}
}

package HomeworkLib.poly;

public class Carriage extends Manual {
public Carriage() {
super();
this.powerSource=PowerSource.HORSE;
}
}

package HomeworkLib.poly;

public class RollerStakes extends Manual{

public RollerStakes(int wheelCount) {
super();
this.wheelCount =wheelCount;
this.powerSource=PowerSource.HUMAN;
this.occupantCount=1;
}

}

package HomeworkLib.poly;

public class Sliding extends Manual {
protected int surfaceCount;

public Sliding(int surfaceCount) {
this.surfaceCount = surfaceCount;
}

@Override
public String toString() {
return "Sliding{" +
"surfaceCount=" + surfaceCount +
"} " + super.toString();
}
}

package HomeworkLib.poly;

public class IceStake extends Sliding {

public IceStake() {
super(2);
this.powerSource =PowerSource.HUMAN;
this.occupantCount=1;
}

}

package HomeworkLib.poly;

public class Skis extends Sliding{
public Skis() {
super(2);
this.powerSource=PowerSource.GRAVITY;
this.occupantCount=1;
}
}

package HomeworkLib.poly;

public class Snowboard extends Sliding {
public Snowboard() {
super(1);
this.powerSource =PowerSource.GRAVITY;
this.occupantCount=1;
}
}

package HomeworkLib.poly;

public class Sleigh extends Sliding {
public Sleigh() {
super(2);
this.powerSource=PowerSource.HORSE;
}
}

Add a comment
Know the answer?
Add Answer to:
Project 7: Vehicles 1 Objective In the last couple projects, you’ve created and used objects in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • We are going to create a few different classes in this project. I will supply one...

    We are going to create a few different classes in this project. I will supply one of the classes: Pound (as in a pound for animals). Your goal will be to create proper Dog and Cat classes that will work in conjunction with this Pound class. Before starting to the design process for the Dog/Cat classes, carefully inspect this code. Get an idea of the goal of the Cat/Dog classes. How many fields will they store? What will the constructors...

  • Y. Daniel Liang’s 8 Class Design Guidelines were posted on the File Manager and handed out...

    Y. Daniel Liang’s 8 Class Design Guidelines were posted on the File Manager and handed out in class. Please choose 5 guidelines and discuss them in depth. For each guideline, use 1 page or more for your discussion. You can use the code provided in class to demonstrate your points. The code should not be more than one-third of your writing. 1. Cohesion • [✓] A class should describe a single entity, and all the class operations should logically fit...

  • PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is...

    PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is the output of the program as it is written? (Program begins on p. 2) 2. Why would a programmer choose to define a method in an abstract class (such as the Animal constructor method or the getName()method in the code example) vs. defining a method as abstract (such as the makeSound()method in the example)? /********************************************************************** *           Program:          PRG/421 Week 1 Analyze Assignment *           Purpose:         Analyze the coding for...

  • UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships...

    UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships Labwork Please read all of the directions carefully. You will create a UML class diagram reflecting the class hierarchy for a fictional program that manages university personnel as constructed according to the graph displayed below. You will need to think about the attributes and behaviors that are unique to each class, and also those attributes and behaviors that are common amongst the subclasses and...

  • Problem You work for a dealership that deals in all kinds of vehicles. Cars, Trucks, Boats,...

    Problem You work for a dealership that deals in all kinds of vehicles. Cars, Trucks, Boats, and so forth need to be inventoried in the system. The inventory must be detailed so that it could be searched based on number of doors, engine type, color, and so on. This program will demonstrate the following: How to create a base class How to extend a based class to create new classes How to use derived classes Solving the Problem Step 1...

  • CIS247C Week 3 Project Overview The objective of this week is to enhance last week's Vehicle cla...

    CIS247C Week 3 Project Overview The objective of this week is to enhance last week's Vehicle class by making the following changes: • Create a static variable called numVehicles that holds an int and initialize it to zero. This will allow us to count all the Vehicle objects created in the main class. • Add the copy constructor • Increment numVehicles in all of the constructors • Decrement numVehicle in destructor • Add overloaded versions of setYear and setMpg that...

  • 1.     When a sub class object is created, when is the call to the super class...

    1.     When a sub class object is created, when is the call to the super class constructor made? How does a programmer call the super class constructor from the sub class? What do all classes indirectly extend? What methods does every class inherit from the Object class? 2.     When writing methods in a sub class, how can those methods call the methods from the parent class? 3.     Which class is more specific, a super class or a sub class? 4.    ...

  • Hi I need help creating a program in JAVA. This is material from chapter 9 Objects...

    Hi I need help creating a program in JAVA. This is material from chapter 9 Objects and Classes from the Intro to JAVA textbook. Mrs. Quackenbush's Rent-A-Wreck car rental ..... Mrs. Q asked you to create a program that can help her track her growing fleet of junkers. Of course she expects you to use your best programming skills, including: 1. Plan and design what attributes/actions an automobile in a rental fleet should have (example. Track number of Trucks, SUVs,...

  • In c# So far, you have created object-oriented code for individual classes. You have built objects...

    In c# So far, you have created object-oriented code for individual classes. You have built objects from these classes. Last week, you created a UML for new inherited classes and objects. Finally, you have used polymorphism. When you add abstraction to this mix, you have the “4 Pillars of OOP.” Now, you begin putting the pieces together to create larger object-oriented programs. Objectives for the project are: Create OO classes that contain inherited and polymorphic members Create abstracted classes and...

  • Python 3 Problem: I hope you can help with this please answer the problem using python...

    Python 3 Problem: I hope you can help with this please answer the problem using python 3. Thanks! Code the program below . The program must contain and use a main function that is called inside of: If __name__ == “__main__”: Create the abstract base class Vehicle with the following attributes: Variables Methods Manufacturer Model Wheels TypeOfVehicle Seats printDetails() - ABC checkInfo(**kwargs) The methods with ABC next to them should be abstracted and overloaded in the child class Create three...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT