Question

Copy the program AmusementRide.java to your computer and add your own class that extends class AmusementRide....

Copy the program AmusementRide.java to your computer and add your own class that extends class AmusementRide.

Note: AmusementRide.java contains two classes (class FerrisWheel and class RollerCoaster) that provide examples for the class you must create.

Your class must include the following.

  • Implementations for all of the abstract methods defined in abstract class AmusementRide.

  • At least one static class variable and at least one instance variable that are not defined in abstract class AmusementRide.

  • Override the inherited repair() method following the instructions given in the repair() method comment block.

  • Override the inherited toString() that returns a String representation of your ride objects. The String must contain the String returned from the inherited toString() method.

The main() method in abstract class AmusementRide must keep the class FerrisWheel and class RollerCoaster objects. The main() method must also keep the following behavior:

   // pseudo-code...
   obj.load() 
   if (obj.start())
      obj.stop()
   obj.repair()

However, you must change the main() method to use polymorphism. [hint: array]

The first six lines of the program's output must match the following.

Ferris wheel "The Billy Preston" has capacity of 50 and costs $6.28
...height: 100; # of spins: 0
mechanics are on strike
Roller coaster "for(;;) Young" has capacity of 32 and costs $3.14
...maxSpeed: 99; trackLength: 25; whiplashFactor: 1.618
mechanics are on strike

Example Output

CSC205 student Edith Foogooman created class BumperCar and she named her ride "Self-driving Bumpers". The output of her program looked as follows.

Ferris wheel "The Billy Preston" has capacity of 50 and costs $6.28
...height: 100; # of spins: 0
mechanics are on strike
Roller coaster "for(;;) Young" has capacity of 32 and costs $3.14
...maxSpeed: 99; trackLength: 25; whiplashFactor: 1.618
mechanics are on strike
Bumper cars "Self-driving Bumpers" has capacity of 17 and costs $6.28
...weight: 13; speed: 8; os: Linux
Self-driving Bumpers has been loaded
Self-driving Bumpers has started
Self-driving Bumpers has stopped
Self-driving Bumpers has been repaired

////////////////////////////////////////////////////

AmusementRide.java:

import java.util.*;

/*
 * AmusementRide is a generalization of ride objects
 * found at amusement parks.
 * 
 * Note: The non-public fields for this class are 
 * defined at the bottom of the class.
 *
 * @creator gdt
 * @created 02018.01.02
 * @updated 02019.02.01  morphed for spring 2019 semester
 */

public abstract class AmusementRide extends Object {

   public final static String DFLT_NAME = "Ride With No Name";
   public final static double DFLT_PRICE = 0.0;
   public final static int DFLT_CAPACITY = 0;

   /**
    * Construct an AmusementRide object.
    *
    * @param n the name of the AmusementRide
    * @param p the price of the AmusementRide
    * @param c the capacity of the AmusementRide
    */
   public AmusementRide(String n, double p, int c) {
      name = (n == null) ? DFLT_NAME : n;
      price = (p < 0) ? DFLT_PRICE : p;
      capacity = (c < 0) ? DFLT_CAPACITY : c;
   }

   /**
    * Print a message indicating this ride has started.
    * The message must include the ride's name.
    *
    * @return  true
    */
   public abstract boolean start(); 

   /**
    * Print a message indicating this ride has stopped.
    * The message must include the ride's name.
    */
   public abstract void stop(); 

   /**
    * Print a message indicating this ride has been loaded.
    * The message must include the ride's name.
    *
    * @return  this ride's capacity
    */
   public abstract int load();

   /**
    * Print a message indicating this ride as been repaired.
    * The message must include the ride's name.
    */
   public void repair() {
      System.out.println("mechanics are on strike");
   }


   /**
    * Set the capacity of this AmusementRide.
    *
    * @param maxload  maximum number of riders allowed
    * @return this AmusementRide
    */
   public AmusementRide setCapacity(int maxload) { 
      capacity = (maxload < 0) ? DFLT_CAPACITY : maxload;
      return this; 
   }

   /**
    * Set the price of this AmusementRide.
    *
    * @param price  is USD
    */
   public AmusementRide setPrice(double p) { 
      price = (p < 0) ? DFLT_PRICE : p;
      return this; 
   }

   /**
    * construct a String representation of this ride...
    *
    * @return a String representation of this AmusementRide
    */
   public String toString() {
      return "\"" + name + "\" has capacity of " + capacity + 
             " and costs $" + price;
   }

   /**
    * main() method used has a driver program to test
    * the methods defined in this class...
    */
   public static void main(String[] argv) {

      FerrisWheel f = new FerrisWheel("The Billy Preston", 50, 6.28, 100);
      RollerCoaster r = new RollerCoaster("for(;;) Young", 32, 3.14,
                                          25, 99, 1.618);

      System.out.println(f);
      f.load();
      if (f.start())
         f.stop();
      f.repair();
      System.out.println(r);
      r.load();
      if (r.start()) 
         r.stop();
      r.repair();
   }

   /*
    * The windSpeed is the same for all AmusementRide objects; 
    * therefore, class variable used instead of an instance variable.
    */
   private static int windSpeed;

   /*
    * The instance data for an AmusementRide.
    */
   protected int capacity; 
   protected String name; 
   private double price; 
   private Date lastRepair = new Date();
}

class FerrisWheel extends AmusementRide {

   /**
    * initialize this object using client supplied data...
    */
   public FerrisWheel(String name, int capacity, double price, int height) {
      super(name, price, capacity);
      if (height <= 0)
         throw new IllegalArgumentException("invalid height");
      this.height = height;
   }

   /**
    * Return a string representation of this object including
    * the state of the AmusementRide part of the object.
    */
   public String toString() {
      return "Ferris wheel " + super.toString() + "\n" +
             "...height: " + height + "; # of spins: " + nSpins;
   }

   /**
    * Spin this ferris wheel once.
    */
   public void spin() { nSpins++; }

   public boolean start() { return true; }
   public void stop() { return; }
   public int load() { return capacity; }

   /*
    * default state values...
    */
   private final static int DFLT_MAX_SPINS = 100;
   private final static int DFLT_HEIGHT = -1;

   /*
    * instance variables...
    */
   private int nSpins = 0;
   private int height = DFLT_HEIGHT;    //in feet

}

class RollerCoaster extends AmusementRide {

   /**
    */
   public RollerCoaster(String name, int capacity, double price,
                        int len, int speed, double factor) {
      super(name, price, capacity);
      init(len, speed, factor);
   }

   private void init(int len, int speed, double factor) {
      if (len < 0)
         throw new IllegalArgumentException("invalid length");
      trackLength = len;
      if (!setMaxSpeed(speed))
         throw new IllegalArgumentException("invalid speed");
      if (factor < 0)
         throw new IllegalArgumentException("invalid factor");
      whiplashFactor = factor;
   }

   /**
    * maxSpeed is not changed if parameter speed is negative
    *
    * @param speed  new speed
    * @return true  if speed changed; false otherwise
    */
   public boolean setMaxSpeed(int speed) {
      if (speed < 0)
         return false;
      maxSpeed = speed;
      return true;
   }

   /**
    * Return a string representation of this object including
    * the state of the AmusementRide part of the object.
    */
   public String toString() {
      final String SEP = "; ";
      return "Roller coaster " + super.toString() + "\n" +
             "...maxSpeed: " + maxSpeed + 
             "; trackLength: " + trackLength + 
             "; whiplashFactor: " + whiplashFactor;
   }

   public boolean start() { return true; }
   public void stop() { return; }
   public int load() { return capacity; }
      
   /*
    * default state values...
    */
   private final static int DFLT_TRACK_LENGTH = -1;
   private final static int DFLT_MAX_SPEED = 0;
   private final static double DFLT_WHIPLASH_FACTOR = 0.0;

   /*
    * instance variables...
    */
   private int trackLength = DFLT_TRACK_LENGTH;             //in feet
   private int maxSpeed = DFLT_MAX_SPEED;                   //in knots
   private double whiplashFactor = DFLT_WHIPLASH_FACTOR;    //gforces

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE :

import java.util.*;

public abstract class AmusementRide extends Object {

public final static String DFLT_NAME = "Ride With No Name";

public final static double DFLT_PRICE = 0.0;

public final static int DFLT_CAPACITY = 0;

/**

* Construct an AmusementRide object.

*

* @param n

* the name of the AmusementRide

* @param p

* the price of the AmusementRide

* @param c

* the capacity of the AmusementRide

*/

public AmusementRide(String n, double p, int c) {

name = (n == null) ? DFLT_NAME : n;

price = (p < 0) ? DFLT_PRICE : p;

capacity = (c < 0) ? DFLT_CAPACITY : c;

}

/**

* Print a message indicating this ride has started.

*

* @return true

*/

public abstract boolean start();

/**

* Print a message indicating this ride has stopped.

*

* @return true

*/

public abstract boolean stop();

/**

* Print a message indicating this ride has been loaded.

*

* @return this ride's capacity

*/

public abstract int load();

/**

* Print a message indicating this ride as been repaired.

*/

public void repair() {

System.out.println("mechanics are on strike");

}

/**

* Set the capacity of this AmusementRide.

*

* @param maxload

* maximum number of riders allowed

* @return this AmusementRide

*/

public AmusementRide setCapacity(int maxload) {

capacity = (maxload < 0) ? DFLT_CAPACITY : maxload;

return this;

}

/**

* Set the price of this AmusementRide.

*

* @param price

* is USD

*/

public AmusementRide setPrice(double p) {

price = (p < 0) ? DFLT_PRICE : p;

return this;

}

/**

* construct a String representation of this ride...

*

* @return a String representation of this AmusementRide

*/

public String toString() {

return "\"" + name + "\" has capacity of " + capacity + " and costs $" + price;

}

/**

* main() method used has a driver program to test the methods defined in this

* class...

*/

public static void main(String[] argv) {

FerrisWheel f = new FerrisWheel("The Billy Preston", 50, 6.28, 100);

RollerCoaster r = new RollerCoaster("for(;;) Young", 32, 3.14, 25, 99, 1.618);

MyClass m = new MyClass("My ride", 66, 9);

/*System.out.println(f);

f.load();

if (f.start())

f.stop();

else

f.repair();

System.out.println(r);

r.load();

if (r.start())

r.stop();

else

r.repair();

System.out.println(m);

m.load();

if (m.start())

m.stop();

else

m.repair();*/

AmusementRide[] rideArray = new AmusementRide[3];

rideArray[0] = new FerrisWheel("The Billy Preston", 50, 6.28, 100);

rideArray[1] = new RollerCoaster("for(;;) Young", 32, 3.14, 25, 99, 1.618);

rideArray[2] = new MyClass("My ride", 66, 9);

for(int i=0;i<rideArray.length;i++)

{

System.out.println(rideArray[i]);

rideArray[i].load();

if (rideArray[i].start())

rideArray[i].stop();

else

rideArray[i].repair();

}

}

/*

* The windSpeed is the same for all AmusementRide objects; therefore, class

* variable used instead of an instance variable.

*/

private static int windSpeed;

/*

* The instance data for an AmusementRide.

*/

private double price;

protected int capacity;

private String name;

private Date lastRepair = new Date();

}

class FerrisWheel extends AmusementRide {

/**

* initialize this object using client supplied data...

*/

public FerrisWheel(String name, int capacity, double price, int height) {

super(name, price, capacity);

if (height <= 0)

throw new IllegalArgumentException("invalid height");

this.height = height;

}

/**

* Return a string representation of this object including the state of the

* AmusementRide part of the object.

*/

public String toString() {

return "Ferris wheel " + super.toString() + "\n" + "...height: " + height + "; # of spins: " + nSpins;

}

/**

* Spin this ferris wheel once.

*/

public void spin() {

nSpins++;

}

public boolean start() {

return true;

}

public boolean stop() {

return true;

}

public int load() {

return capacity;

}

/*

* default state values...

*/

private final static int DFLT_MAX_SPINS = 100;

private final static int DFLT_HEIGHT = -1;

/*

* instance variables...

*/

private int nSpins = 0;

private int height = DFLT_HEIGHT; // in feet

}

class RollerCoaster extends AmusementRide {

/**

*/

public RollerCoaster(String name, int capacity, double price, int len, int speed, double factor) {

super(name, price, capacity);

init(len, speed, factor);

}

private void init(int len, int speed, double factor) {

if (len < 0)

throw new IllegalArgumentException("invalid length");

trackLength = len;

if (!setMaxSpeed(speed))

throw new IllegalArgumentException("invalid speed");

if (factor < 0)

throw new IllegalArgumentException("invalid factor");

whiplashFactor = factor;

}

/**

* maxSpeed is not changed if parameter speed is negative

*

* @param speed

* new speed

* @return true if speed changed; false otherwise

*/

public boolean setMaxSpeed(int speed) {

if (speed < 0)

return false;

maxSpeed = speed;

return true;

}

/**

* Return a string representation of this object including the state of the

* AmusementRide part of the object.

*/

public String toString() {

final String SEP = "; ";

return "Roller coaster " + super.toString() + "\n" + "...maxSpeed: " + maxSpeed + "; trackLength: "

+ trackLength + "; whiplashFactor: " + whiplashFactor;

}

public boolean start() {

return true;

}

public boolean stop() {

return true;

}

public int load() {

return capacity;

}

/*

* default state values...

*/

private final static int DFLT_TRACK_LENGTH = -1;

private final static int DFLT_MAX_SPEED = 0;

private final static double DFLT_WHIPLASH_FACTOR = 0.0;

/*

* instance variables...

*/

private int trackLength = DFLT_TRACK_LENGTH; // in feet

private int maxSpeed = DFLT_MAX_SPEED; // in knots

private double whiplashFactor = DFLT_WHIPLASH_FACTOR; // gforces

}

//Created class myclass

class MyClass extends AmusementRide{

public MyClass(String name, double price, int capacity) {

super(name, price, capacity);

// TODO Auto-generated constructor stub

}

@Override

public boolean start() {

// TODO Auto-generated method stub

return false;

}

@Override

public boolean stop() {

// TODO Auto-generated method stub

return false;

}

@Override

public int load() {

// TODO Auto-generated method stub

return 0;

}

/**

* Return a string representation of this object including the state of the

* AmusementRide part of the object.

*/

public String toString() {

return "MyClass " + super.toString() + "\n" + "maxTurn: " + maxTurn ;

}

//one static variable

private static int DFLT_MAX_TURN=0;

//one instace variable

private int maxTurn = DFLT_MAX_TURN;

}

Add a comment
Know the answer?
Add Answer to:
Copy the program AmusementRide.java to your computer and add your own class that extends class AmusementRide....
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
  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • Write in java and the class need to use give in the end copy it is...

    Write in java and the class need to use give in the end copy it is fine. Problem Use the Plant, Tree, Flower and Vegetable classes you have already created. Add an equals method to Plant, Tree and Flower only (see #2 below and the code at the end). The equals method in Plant will check the name. In Tree it will test name (use the parent equals), and the height. In Flower it will check name and color. In...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • COVERT TO PSEUDOCODE /****************************Vacation.java*****************************/ public abstract class Vacation {    /*    * private data field...

    COVERT TO PSEUDOCODE /****************************Vacation.java*****************************/ public abstract class Vacation {    /*    * private data field    */    private double cost;    private double budget;    private String destination;    /**    *    * @param cost    * @param budget    * @param destination    */    public Vacation(double cost, double budget, String destination) {        super();        this.cost = cost;        this.budget = budget;        this.destination = destination;    }    //getter and...

  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...

  • DO NOT COPY AND PASTED!!!!!!!!!!!!! IF YOU DO NOT HOW TO DO IT, JUST DO NOT...

    DO NOT COPY AND PASTED!!!!!!!!!!!!! IF YOU DO NOT HOW TO DO IT, JUST DO NOT ANSWER MY QUESTIONS package scheduler; import java.util.List; public class Scheduler { /** * Instantiates a new, empty scheduler. */ public Scheduler() { } /** * Adds a course to the scheduler. * * @param course the course to be added */ public void addCourse(Course course) { } /** * Returns the list of courses that this scheduler knows about. * * This returned object...

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