Question

I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java...

I need help writing my main method****

Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #4 – Classes and Objects (20 Points) You will create 3 new classes for this project, two will be chosen from the list below and one will be an entirely new class you invent.Here is the list: Shirt Shoe Wine Book Song Bicycle VideoGame Plant Car FootBall Boat Computer WebSite Movie Beer Pants TVShow MotorCycle Design First Create three (3) UML diagrams for three classes. Two of the classes are assigned by the instructor from the above list and one you must invent. Choose as the third class, something you know very well or love doing. If soccer is your favorite sport, create a class Soccer. If you sing opera, create a class Opera. Do not overthink. Just come up with the 3 most important data members (i.e. global variables) that describe the class/object you want to build and the design 2 constructors, 3 get methods, 3 set methods and a toString method and you are done. Please do not use the same 3 data types for any classes global variables. That is, do not make them all ' int ' or all ' String '. Next to each UML diagram decide on a default value, minimum value and a maximum value for EACH numeric variable. For example, the Shoe class and its size variable may have a default value of 0; the minimum size may be 1 and the maximum size 15. A String variable may set a default value of null, and a minimum and maximum length of the String. For example, the Computer class and its brand variable may have a default value of null, a minimum string length of 3 and the maximum length 20. Please do not use the same 3 data types for any classes global variables. That is, do not make them all ' int ' or all ' String '. Implement Next Create a NetBeans project. Create a Java file for EACH of the three classes. For example, add to your project a new file called Book.java and then create the new class from scratch in that file. Use your UML diagrams as the guideline for writing the code. The variables and methods from the diagrams will be part of each of your classes. Make sure ALL your variables are declared to be private. Protect Your Data! Objects store data or information! When variables are declared private you can protect or guard that information like a Pit Bull protects a piece of meat. Never allow bad data to be stored in your objects! In each 'set' method, make sure the value passed to the method is in range, greater than or equal to the minimum and less than or equal to the maximum. For strings, you may check the length of the string. Each 'set' method should have some sort of 'if-else' statement, assign the data when it is good and print an informative message when an incorrect value is passed. The Shoe class setSize() method would assign the value '10' to the size global variable when it is passed to the method. But, it would print a 'Shoe size must be between 1 and 15' and NOT change the global variable when a value such as '437' was passed to the method. The private variable declarations build a wall around your data, and the 'set' methods are the gates that allow only 'good' information in. Your constructor that assigns values to global variables should use the 'set' methods so you DO NOT have to repeat the same checks in the constructor. The constructor with NO parameters can go ahead and directly set the default values into the global variables. Test Next For each class, create a main method that will declare, build and use an object of that class. So the Book.java main will declare, build and use a Book object, and the other two classes will do the same. Use a command line interface and ask the user to input a value for EACH global variable. Call a constructor or the set methods and insert that information into the object. Once the data is inserted use the object to call the toString method and print the object to the console. You will be writing THREE main methods, one for each class. When you test, make sure your set methods DO NOT allow bad data into the object. Try to make it fail, see if you can sneak bad values into the variables.

I need help writing my main method for the Car. Here is my code so far

package project4;

public class Car {

private static final int MIN_SPEED = 0;

private static final int MAX_SPEED = 300;

private int modelNumber;

private String color;

private double speed;

public Car(int modelNumber) {

this.modelNumber = modelNumber;

this.speed = 0;

}

public Car(int modelNumber, String color, double speed) {

this.modelNumber = modelNumber;

this.color = color;

this.speed = speed;

}

public int getModelNumber() {

return modelNumber;

}

public void setModelNumber(int modelNumber) {

this.modelNumber = modelNumber;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public double getSpeed() {

return speed;

}

public void setSpeed(double speed) {

if (speed < MIN_SPEED && speed >MAX_SPEED) {

System.out.println("Speed out of range!\n");

}

this.speed = speed;

}

public void accelerate() {

this.speed += 5;

}

public void brake() {

this.speed = 0;

}

@Override

public String toString() {

return "Car [modelNumber=" + modelNumber + ", color=" + color + ", speed=" + speed + "]";

}

public static void main(String[] args) {

}

}

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

Screenshot

Program

/**
* Create a class With attributes of model number,color and speed
* Set max and min speed as final variables
* Appropriate constructors ,getters and setters and override toString method
* Main method to test class
* @author deept
*
*/
public class Car {
   //Attributes
   private static final int MIN_SPEED = 0;
   private static final int MAX_SPEED = 300;
   private int modelNumber;
   private String color;
   private double speed;
   //Constructor set model ,speed min speed and color Blue
   public Car(int modelNumber) {
       this.modelNumber = modelNumber;
       this.speed = 0;
       this.color="Blue";
   }
   public Car(int modelNumber, String color, double speed) {
       this.modelNumber = modelNumber;
       this.color = color;
       setSpeed(speed);
   }
   //Getter for mpdel number
   public int getModelNumber() {
       return modelNumber;
   }
   //Setter for model number
   public void setModelNumber(int modelNumber) {

       this.modelNumber = modelNumber;
   }
   //Getter for color
   public String getColor() {
       return color;
   }
   //Setter for color
   public void setColor(String color) {
       this.color = color;
   }
   //Getter for speed
   public double getSpeed() {
       return speed;
   }
   //Setter for speed
   public void setSpeed(double speed) {
       if (speed < MIN_SPEED || speed >MAX_SPEED) {
           System.out.println("\nSpeed out of range! so set it min speed\n");
           this.speed=MIN_SPEED;
       }
       else {
           this.speed = speed;
       }
   }
   //Accelerate speed
   //Increment 5 until read MAX_SPEED
   public void accelerate() {
       if(this.speed+5<=MAX_SPEED) {
           this.speed += 5;
       }
       else {
           System.out.println("\nSpeed cannot accelerate, already reach maximum speed\n");
       }
   }
   //Apply break read speed 0
   public void brake() {
       this.speed = 0;
   }
   @Override
   public String toString() {
       return "Car [modelNumber=" + modelNumber + ", color=" + color + ", speed=" + speed + "]";
   }
   //Test
   public static void main(String[] args) {
       //Create class object using first constructor
       Car car1=new Car(2018);
       //Display details of car
       System.out.println("First constructor Test: "+car1);
       //Create class object using second constructor
       Car car2=new Car(2018,"Red",-150);
       //Display details of car
       System.out.println("Second constructor Test: "+car2);
       //Setter test
       car2.setSpeed(100);;
       //Display details of car after set
       System.out.println("Speed setter Test: "+car2);
       //Accelerate test
       car2.accelerate();
       System.out.println("Accelerate Test: "+car2);
       //Color setter and getter test
       car1.setColor("Grey");
       System.out.println("Setter and getter of color test: "+car1);
       //Accelerate error detection
       car1.setSpeed(296);
       car1.accelerate();
       System.out.println("accelerate error test: "+car1);
   }

}

---------------------------------------------------------------------

Output

First constructor Test: Car [modelNumber=2018, color=Blue, speed=0.0]

Speed out of range! so set it min speed

Second constructor Test: Car [modelNumber=2018, color=Red, speed=0.0]
Speed setter Test: Car [modelNumber=2018, color=Red, speed=100.0]
Accelerate Test: Car [modelNumber=2018, color=Red, speed=105.0]
Setter and getter of color test: Car [modelNumber=2018, color=Grey, speed=0.0]

Speed cannot accelerate, already reach maximum speed

accelerate error test: Car [modelNumber=2018, color=Grey, speed=296.0]

-----------------------------------------------------------------------------

Note:-

I assume you are expecting this way

Add a comment
Know the answer?
Add Answer to:
I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java...
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
  • Computer Science 111 Introduction to Algorithms and Programming: Java Programming Net Beans Project #4 - Classes...

    Computer Science 111 Introduction to Algorithms and Programming: Java Programming Net Beans Project #4 - Classes and Objects (15 Points) You will create 3 new classes for this project, two will be chosen (THE TWO CHOSEN ARE TENNIS SHOE AND TREE) from the list below and one will be an entirely new class you invent. Here is the list: Cellphone Clothes JuiceDrink Book MusicBand Bike GameConsole Tree Automobile Baseball MusicPlayer Laptop TennisShoe Cartoon EnergyDrink TabletComputer RealityShow HalloweenCostume Design First Create...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • fisrt one is bicycle.java second code is bicycleapp.java can anyone help this??? please save my life...

    fisrt one is bicycle.java second code is bicycleapp.java can anyone help this??? please save my life Before you begin, DOWNLOAD the files “Lab8_Bicycle.java” and “Lab8_BicycleApp.java” from Canvas to your network drive (not the local hard drive or desktop). Once you have the files saved, RENAME THE FILES Bicycle.java and BicycleApp.java then open each file. 1) Look at the Bicycle class. Two of the many data properties we could use to define a bike are its color and the gear it...

  • Hello, In need of help with this Java pa homework. Thank you! 2. Create a normal...

    Hello, In need of help with this Java pa homework. Thank you! 2. Create a normal (POJo, JavaBean) class to represent, i. e. model, varieties of green beans (also known as string beans or snap beans). Following the steps shown in "Assignment: Tutorial on Creating Classes in IntelliJ", create the class in a file of its own in the same package as class Main. Name the class an appropriate name. The class must have (a) private field variables of appropriate...

  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors •...

    Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors • Be able to create accessor and mutator methods • Be able to create toString methods • Be able to refer to both global and local variables with the same name Introduction Until now, you have just simply been able to refer to any variable by its name. This works for our purposes, but what happens when there are multiple variables with the same name?...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • 7. Programming Problem: In this problem we are trying to model a repair service that can...

    7. Programming Problem: In this problem we are trying to model a repair service that can repair Cars and Computers. Even though the Car and Computer are different products, they have common repair methods that can be abstracted in an interface. Please complete the interface and the classes by adding the required methods, as per the problem description. Partially complete code is provided as shown below A. Create an interface called Mechanism. This interface has two methods. The first method...

  • Hi I have 4 problems below I need done ASAP, if you could upload them one...

    Hi I have 4 problems below I need done ASAP, if you could upload them one by one by order you finish them instead of all at once that would be appreciated, I need this done in basic c++ code, all of the body structure are below I just need codes to be implemented for the codes to give the write outputs, Thank you. Q1. Update the given code 1 to implement the following problem: Create a class animal Set...

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