Question

Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name...

Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name Access modifier: private Data Type: String Name: currentWeight Access modifier: private Data Type: double Name: maximumWeight Access modifier: private Data Type: double Constructors Name: Bag Access modifier: public Parameters: none (default constructor) Task: sets the value of the instance variable name to the empty string sets the value of the instance variable currentWeight to 0.0 sets the value of the instance variable maximumWeight to 5.0 Methods Name: setName Access modifier: public Parameters: newName Return Type: void Task: sets the value of the instance variable name to newName Name: getName Access modifier: public Parameters: none Return Type: String Task: returns the value of the instance variable name Name: addItem Access modifier: public Parameters: newWeight Return Type: void Task: sets the value of the instance variable currentWeight equal to the value of currentWeight plus the value of newWeight if the value of newWeight is greater than 0 and if the value of currentWeight plus the value of newWeight is less than or equal to the value of the instance variable maximumWeight Name: getCurrentWeight Access modifier: public Parameters: none Return Type: double Task: returns the value of the instance variable currentWeight Name: setMaximumWeight Access modifier: public Parameters: newMaximumWeight Return Type: void Task: sets the value of the instance variable maximumWeight to value of the newMaximumWeight if newMaximumWeight is greater than 0 and greater than or equal to the value of the instance variable currentWeight Name: getMaximumWeight Access modifier: public Parameters: none Return Type: double Task: returns the value of the instance variable maximumWeight Step 2 Develop a class with only a main method in it: import java.util.Scanner; public class BagDemo { public static void main(String[] args) { //Create a Scanner object called keyboard that takes input from //System.in //Create an object of the Bag class refer to this object as myBag //declare a variable called option of type int //Open a do/while loop //Prompt the user to pick one of the following options: //Press 1 to change the name of the bag //Press 2 to add an item to the bag //Press 3 to change the maximum weight of the bag //Press 4 to view all information about the bag //Press 5 to end the program //Save the user’s input into the option variable //if the user picks option 1, prompt the user for the name of the bag //then save the name of the bag in a variable called newName //change the name of the bag to newName //else if the user picks option 2, prompt the user for the weight //of the item and then save the weight of the item in a variable //called newWeight //add the new item to the bag //else if the user picks option 3, prompt the user for the new maximum //weight of the bag and save the new maximum weight in a variable //called newMaximumWeight //change the maximum weight of the bag to newMaximumWeight //else if the user picks option 4, display to the screen the name of //the bag, the current weight of the bag, and the maximum weight //of the bag //else if the user picks option 5, display Goodbye. //else if the user picks any other option, display Error! //close the do/while loop and make it so that it continues to run as //long as the user does not pick option 5 } }

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

/*This is the required Java program.  */

import java.io.*;
import java.util.Scanner;

public class Bag {
private String name;
private double currentWeight;
private double maximumWeight;
  
public Bag(){ /* Default Constructor*/
name = "";
currentWeight = 0.0;
maximumWeight = 5.0;
}
  
public void setName(String newName){ /*Setter method for setting Bag name*/
name = newName;
}
  
public String getName(){ /*Getter method for getting Bag name*/
return name;
}
  
public void addItem(double newWeight){ /*Method for adding new weight*/
if(newWeight>0 && (currentWeight+newWeight)<=maximumWeight)
currentWeight+=newWeight;
}
  
public double getCurrentWeight(){
return currentWeight;
}
  
public void setMaximumWeight(double newMaximumWeight){ /*Method for setting maximum weight*/
if(newMaximumWeight>0 && newMaximumWeight>=currentWeight)
maximumWeight = newMaximumWeight;
}
  
public double getMaximumWeight(){
return maximumWeight;
}
}
public class BagDemo{ /*BagDemo class with only one method,i.e., main method*/
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
Bag myBag = new Bag();
int option;
  
do{
System.out.println("Press 1 to change the name of the bag");
System.out.println("Press 2 to add an item to the bag");
System.out.println("Press 3 to change the maximum weight of the bag");
System.out.println("Press 4 to view all information about the bag");
System.out.println("Press 5 to end the program");
  
option = keyboard.nextInt();
switch(option){
case 1: System.out.println("Give a new name for the bag"); /*Case for giving new name to the bag*/
keyboard.nextLine();
String newName = keyboard.nextLine();
myBag.setName(newName);
break;
case 2: System.out.println("Enter the weight of the Item"); /*Case for adding a new weight*/
double newWeight = keyboard.nextDouble();
myBag.addItem(newWeight);
break;
case 3: System.out.println("Enter the new maximum weight"); /*Case for getting maximum weight*/
double newMaximumWeight = keyboard.nextDouble();
myBag.setMaximumWeight(newMaximumWeight);
break;
case 4: System.out.println("Name of bag: "+myBag.getName()+" "+"Current weight: "+myBag.getCurrentWeight()+" "+"Maximum weight: "+myBag.getMaximumWeight()); /*Case for displaying the details of the bag*/
break;
case 5: System.out.println("Goodbye"); /*Case for Exiting*/
break;
default: System.out.println("Error!"); /* Case for any input other than 1,2,3,4,5 */
break;
}
  
}while(option!=5 );
  
keyboard.close(); /*Closing Scanner*/   

}
}

Add a comment
Know the answer?
Add Answer to:
Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name...
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
  • Step 1Develop the following class:ClassName: CollegeDegreeAccess Modifier: publicInstance variablesName: major...

    Step 1Develop the following class:ClassName: CollegeDegreeAccess Modifier: publicInstance variablesName: majorAccess modifier: privateData type: StringName: numberOfCoursesAccess modifier: privateData type: intName: courseNameArrayAccess modifier: privateData type: String [ ]Name: courseCreditArrayAccess modifier: privateData type: int [ ]Static variablesName: maximumNumberOfCoursesAccess modifier: privateData type: intInitial Value: 40ConstructorName: CollegeDegreeAccess modifier: publicParameters: none (default constructor)Task: sets major to the empty stringSets numberOfCourses to 0Sets courseNameArray to refer to an array of Strings with the number of elements equal to the value of the static variable maximumNumberOfCoursesSets courseCreditArray to...

  • You may not add any instance variables to any class, though you may create local variables...

    You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here.    Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [ ] Name: courseCreditArray Access...

  • You may not add any instance variables to any class, though you may create local variables...

    You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here. (I need step 2 please.) Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [...

  • Programming assignment for Java: Do not add any other instance variables to any class, but you...

    Programming assignment for Java: Do not add any other instance variables to any class, but you can create local variables in a method to accomplish tasks. Do not create any methods other than the ones listed below. Step 1 Develop the following class: Class Name: College Degree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String Name: courseCreditArray Access modifier:...

  • Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major...

    Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [ ] Name: courseCreditArray Access modifier: private Data type: int [ ] Static variables Name: maximumNumberOfCourses Access modifier: private Data type: int Initial Value: 40 Constructor Name: CollegeDegree Access modifier: public Parameters: none (default constructor) Task: sets major to the empty string...

  • Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info...

    Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data type: QueueNode<T> Constructors: Name: QueueNode Access modifier: public Parameters: info (data type T) Task: sets the value of this.info to the value of the info parameter sets the value of link to null Methods Name: setInfo Access modifier: public Parameters: info (data type T) Return type: void Task: sets the...

  • Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables...

    Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: QueueNode<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue                           Access modifier: public Parameters: none Return type: T (parameterized type) Task: makes...

  • java

    You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here.   Step 1Develop the following class:ClassName: CollegeDegreeAccess Modifier: publicInstance variablesName: majorAccess modifier: privateData type: StringName: numberOfCoursesAccess modifier: privateData type: intName: courseNameArrayAccess modifier: privateData type: String [ ]Name: courseCreditArrayAccess modifier: privateData type: int [ ]Static variablesName: maximumNumberOfCoursesAccess modifier: privateData type: intInitial Value: 40ConstructorName: CollegeDegreeAccess modifier: publicParameters: none...

  • Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access...

    Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data...

  • Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access...

    Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data...

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