Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the project (question1 package) you are given object classes for Frames and W

ID: 3791038 • Letter: I

Question

In the project (question1 package) you are given object classes for Frames and Wheels. Your job is to create a new Object class Unicycle that has as data field a Frame object and a Wheel object. It also has a String data field which represents the customer for whom the Unicycle is being built. I suggest taking a few minutes to look over Frame and Wheel to see what they do. Your Unicycle class should have the following: A default, no argument constructor that creates a unicycle with your name as the customer and a frame/wheel combination of your choice. A fully-argumented constructor that takes in a wheel, frame and customer name. A copy constructor that creates a new Unicycle that has the same properties as the incoming argument Unicycle. Get and set methods for all of the data fields. An appropriate toString method. The idea here is to practice deep-copy for your constructors, set methods and return methods. Wherever appropriate you must avoid using shallow copy. Write a hard-coded tester to make sure that your objects are working properly and that there are no "data leaks" (this is what happens when one object changes and those changes are reflected in another). Create an interactive tester class that collects from the user all of the information require to create their own custom Unicycle and allows them to modify if they choose to. My hard-coded tester output: Creating Unicycle using default constructor: Unicycle [Aaron Langille, Frame [16, blue] Wheel [12, learner]] Creating new frame and wheel for another unicycle: Frame [18, green] Wheel [16, mountain] Creating new custom unicycle from the parts for Peter Parker (aka Spiderman): Unicycle [Peter Parker, France [18, green], Wheel[16, mountain]] Pete changed his mind and wants to alter the unicycle to be a red and blue learner: Unicycle [Peter Parker, France [18, red and blue], Wheel [16, learner]]

Explanation / Answer

I created my own Frame and Wheel classes. You can replace them with yours' and also make sure to replace some of the variable names for frame and wheel classes used within the tester class as per requirement.

PROGRAM CODE:

Frame.java

package simple;

public class Frame {

  

   private int number;

   private String color;

  

   public Frame(int number, String color)

   {

       this.number = new Integer(number);

       this.color = new String(color);

   }

  

   public Frame(Frame frame)

   {

       //Already the returning objects are deep copied hence no worries

       this.number = frame.getNumber();

       this.color = frame.getColor();

   }

   public int getNumber() {

       return new Integer(number);

   }

   public void setNumber(int number) {

       this.number = new Integer(number);

   }

   public String getColor() {

       return new String(color);

   }

   public void setColor(String color) {

       this.color = new String(color);

   }

   @Override

   public String toString() {

       // TODO Auto-generated method stub

       return "Frame[" + number + ", " + color + "]";

   }

  

}

Wheel.java

package simple;

public class Wheel {

  

   private int number;

   private String type;

  

   public Wheel(int number, String type)

   {

       this.number = new Integer(number);

       this.type = new String(type);

   }

  

   public Wheel(Wheel wheel)

   {

       this.number = wheel.getNumber();

       this.type = wheel.getType();

   }

   public int getNumber() {

       return new Integer(number);

   }

   public void setNumber(int number) {

       this.number = new Integer(number);

   }

   public String getType() {

       return new String(type);

   }

   public void setType(String type) {

       this.type = new String(type);

   }

  

   @Override

   public String toString() {

       // TODO Auto-generated method stub

       return "Wheel[" + number + ", " + type + "]";

   }

}

Unicycle.java

package simple;

public class Unicycle {

  

   private String customerName;

   private Frame frame;

   private Wheel wheel;

  

   public Unicycle() {

       //fill in with your name here

       customerName = "Your Name";

       frame = new Frame(16, "blue");

       wheel = new Wheel(12, "learner");

   }

  

   public Unicycle(String customerName, Frame frame, Wheel wheel)

   {

       this.customerName = new String(customerName);

       this.frame = new Frame(frame);

       this.wheel = new Wheel(wheel);

   }

  

   public Unicycle(Unicycle that)

   {

       this(that.customerName, that.frame, that.wheel);

   }

   public String getCustomerName() {

       return new String(customerName);

   }

   public void setCustomerName(String customerName) {

       this.customerName = new String(customerName);

   }

   public Frame getFrame() {

       return new Frame(frame);

   }

   public void setFrame(Frame frame) {

       this.frame = new Frame(frame);

   }

   public Wheel getWheel() {

       return new Wheel(wheel);

   }

   public void setWheel(Wheel wheel) {

       this.wheel = new Wheel(wheel);

   }

  

   @Override

   public String toString() {

       // TODO Auto-generated method stub

       return "Unicycle[" + customerName + ", " + frame + ", " + wheel + "]";

   }

  

}  

UnicycleTester.java

package simple;

import java.util.Scanner;

public class UnicycleTester {

   public static void main(String[] args) {

       int choice;

       char wantTocontinue = 'Y';

       Unicycle unicycles[] = new Unicycle[10];

       int size = 0;

       Scanner keyboard = new Scanner(System.in);

       System.out.println("Welcome to creating your own unicycles");

      

       while(wantTocontinue == 'Y' || wantTocontinue=='y')

       {

           System.out.print(" MENU 1. Create a unicycle 2. Modify an existing unicycle Enter your choice: ");

           choice = Integer.valueOf(keyboard.nextLine());

           String customerName, color, type;

           int numberFrame, numberWheel, option, index = 0;

           Unicycle unicycle = null;

           switch(choice)

           {

           case 1: System.out.print(" Enter the customer name: ");

                   customerName = keyboard.nextLine();

                   System.out.print("Enter the frame number: ");

                   numberFrame = Integer.valueOf(keyboard.nextLine());

                   System.out.print("Enter the frame color: ");

                   color = keyboard.nextLine();

                   System.out.print("Enter the wheel number: ");

                   numberWheel = Integer.valueOf(keyboard.nextLine());

                   System.out.print("Enter the wheel type: ");

                   type = keyboard.nextLine();

                   unicycles[size] = new Unicycle(customerName, new Frame(numberFrame, color), new Wheel(numberWheel, type));

                   System.out.println(" Unicycle created: "+ unicycles[size]);

                   size++;

                   break;

           case 2: System.out.print("Enter the customer name: ");

                   customerName = keyboard.nextLine();

                   for(int i=0; i<size; i++)

                   {

                       if(unicycles[i].getCustomerName().equals(customerName))

                       {

                           unicycle = new Unicycle(unicycles[i]);

                           index = i;

                           break;

                       }

                   }

                   if(unicycle == null)

                       System.out.println(" Customer not found !");

                   else

                       {

                       System.out.println(" Customer found: " + unicycle);

                       System.out.println(" 1. Modify Frame color 2. Modify Wheel Type Choose your option: ");

                       option = Integer.valueOf(keyboard.nextLine());

                       if(option == 1)

                       {

                           System.out.println("Enter the new color: ");

                           color = keyboard.nextLine();

                           Frame frame = new Frame(unicycle.getFrame());

                           frame.setColor(color);

                           unicycle.setFrame(frame);;

                           unicycles[index] = new Unicycle(unicycle);

                       }

                       else if(option == 2)

                       {

                           System.out.println("Enter the new type: ");

                           type = keyboard.nextLine();

                           Wheel wheel = new Wheel(unicycle.getWheel());

                           wheel.setType(type);

                           unicycle.setWheel(wheel);

                           unicycles[index] = new Unicycle(unicycle);

                       }

                       System.out.println("After modification: " + unicycles[index]);

                       }

                   break;

           }

           System.out.print(" Do you wish to continue(Y/N): ");

           wantTocontinue = keyboard.nextLine().charAt(0);

       }

   }

}

OUTPUT:

Welcome to creating your own unicycles

MENU

1. Create a unicycle

2. Modify an existing unicycle

Enter your choice: 1

Enter the customer name: Peter Parker

Enter the frame number: 12

Enter the frame color: red

Enter the wheel number: 16

Enter the wheel type: learner

Unicycle created:

Unicycle[Peter Parker, Frame[12, red], Wheel[16, learner]]

Do you wish to continue(Y/N): Y

MENU

1. Create a unicycle

2. Modify an existing unicycle

Enter your choice: 2

Enter the customer name: Peter Parker

Customer found:

Unicycle[Peter Parker, Frame[12, red], Wheel[16, learner]]

1. Modify Frame color

2. Modify Wheel Type

Choose your option:

2

Enter the new type:

trainer

After modification:

Unicycle[Peter Parker, Frame[12, red], Wheel[16, trainer]]

Do you wish to continue(Y/N): N

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote