SlideShare a Scribd company logo
1 of 7
Download to read offline
Please write the class and class with main method clearly!! This is to be written in JAVA.
Step 1 Develop the following class:
Class
Name: Bag
Access Modifier: public
Instance variables
1-Name: name
Access modifier: private
Data Type: String
2-Name: currentWeight
Access modifier: private
Data Type: double
3-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 } }
Solution
HI, Please find my implementation.
Please let me know in case of any issue.
############### Bag.java ########################
public class Bag {
// instance variables
private String name;
private double currentWeight;
private double maximumWeight;
// constructor
public Bag() {
name = "";
currentWeight = 0.0;
maximumWeight = 5.0;
}
public void setName(String newName){
name = newName;
}
public String getName(){
return name;
}
public void addItem(double newWeight){
double total = currentWeight + newWeight;
if(newWeight > 0 && total <= maximumWeight)
currentWeight = total;
}
public double getCurrentWeight(){
return currentWeight;
}
public void setMaximumWeight(double newMaximumWeight){
if(newMaximumWeight >0 && (currentWeight <= newMaximumWeight))
maximumWeight = newMaximumWeight;
}
public double getMaximumWeight(){
return maximumWeight;
}
}
############### BagDemo.java ###############
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
Scanner sc = new Scanner(System.in);
//Create an object of the Bag class refer to this object as myBag
Bag myBag = new Bag();
//declare a variable called option of type int
int option;
//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
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 ");
//Save the user’s input into the option variable
option = sc.nextInt();
//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
if(option == 1){
System.out.print("Enter name: ");
String newName = sc.next();
myBag.setName(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(option == 2){
System.out.print("Enter weight of the item: ");
double newWeight = sc.nextDouble();
myBag.addItem(newWeight);
}
//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(option == 3){
System.out.print("Enter new max weight: ");
double newMaximumWeight = sc.nextDouble();
myBag.setMaximumWeight(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(option == 4){
System.out.println("Name: "+myBag.getName());
System.out.println("Current Weight: "+myBag.getCurrentWeight());
System.out.println("Max Weight: "+myBag.getMaximumWeight());
}
//else if the user picks option 5, display Goodbye.
else if(option == 5){
System.out.println("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
else{
System.out.println("Invalid option!!!");
}
}while(option != 5);
}
}
/*
Sample run:
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
1
Enter name: AmericalTourist
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
2
Enter weight of the item: 34
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
3
Enter new max weight: 50
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
2
Enter weight of the item: 34
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
4
Name: AmericalTourist
Current Weight: 34.0
Max Weight: 50.0
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
5
Goodbye
*/

More Related Content

Similar to Please write the class and class with main method clearly!! This is .pdf

Ive already completed the Java assignment below, but it doesnt wor.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdfIve already completed the Java assignment below, but it doesnt wor.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdf
fantasiatheoutofthef
 
Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdf
Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdfAdapt your code from Assignment 2 to use both a Java API ArrayList a.pdf
Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdf
albarefqc
 
These are the outputs which should match they are 4 of them -outp.pdf
These are the outputs which should match they are 4 of them -outp.pdfThese are the outputs which should match they are 4 of them -outp.pdf
These are the outputs which should match they are 4 of them -outp.pdf
udit652068
 
Code to copy Person.java .pdf
Code to copy Person.java .pdfCode to copy Person.java .pdf
Code to copy Person.java .pdf
anokhijew
 
Here is the assignment5.java file -You are required, but not limi.pdf
Here is the assignment5.java file -You are required, but not limi.pdfHere is the assignment5.java file -You are required, but not limi.pdf
Here is the assignment5.java file -You are required, but not limi.pdf
mallik3000
 
School System C# Code
School System C# CodeSchool System C# Code
School System C# Code
Paul Smyth
 
Hello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdfHello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdf
Ian0J2Bondo
 
Can someoen help me to write c++ program including C++ inheritance, .pdf
Can someoen help me to write c++ program including C++ inheritance, .pdfCan someoen help me to write c++ program including C++ inheritance, .pdf
Can someoen help me to write c++ program including C++ inheritance, .pdf
arrowvisionoptics
 

Similar to Please write the class and class with main method clearly!! This is .pdf (14)

Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Ive already completed the Java assignment below, but it doesnt wor.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdfIve already completed the Java assignment below, but it doesnt wor.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdf
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdf
Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdfAdapt your code from Assignment 2 to use both a Java API ArrayList a.pdf
Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdf
 
Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)
 
These are the outputs which should match they are 4 of them -outp.pdf
These are the outputs which should match they are 4 of them -outp.pdfThese are the outputs which should match they are 4 of them -outp.pdf
These are the outputs which should match they are 4 of them -outp.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
Code to copy Person.java .pdf
Code to copy Person.java .pdfCode to copy Person.java .pdf
Code to copy Person.java .pdf
 
Here is the assignment5.java file -You are required, but not limi.pdf
Here is the assignment5.java file -You are required, but not limi.pdfHere is the assignment5.java file -You are required, but not limi.pdf
Here is the assignment5.java file -You are required, but not limi.pdf
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
 
class object.pptx
class object.pptxclass object.pptx
class object.pptx
 
School System C# Code
School System C# CodeSchool System C# Code
School System C# Code
 
Hello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdfHello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdf
 
Can someoen help me to write c++ program including C++ inheritance, .pdf
Can someoen help me to write c++ program including C++ inheritance, .pdfCan someoen help me to write c++ program including C++ inheritance, .pdf
Can someoen help me to write c++ program including C++ inheritance, .pdf
 

More from udit652068

Did BP respond in a manner that was appropriate with the oil spill t.pdf
Did BP respond in a manner that was appropriate with the oil spill t.pdfDid BP respond in a manner that was appropriate with the oil spill t.pdf
Did BP respond in a manner that was appropriate with the oil spill t.pdf
udit652068
 
Describe and provide at least two examples of direct transmission of.pdf
Describe and provide at least two examples of direct transmission of.pdfDescribe and provide at least two examples of direct transmission of.pdf
Describe and provide at least two examples of direct transmission of.pdf
udit652068
 
Define e-commerce and describe how it differs from e-business.So.pdf
Define e-commerce and describe how it differs from e-business.So.pdfDefine e-commerce and describe how it differs from e-business.So.pdf
Define e-commerce and describe how it differs from e-business.So.pdf
udit652068
 
You are to write a C++ program to produce an inventory repor.pdf
You are to write a C++ program to produce an inventory repor.pdfYou are to write a C++ program to produce an inventory repor.pdf
You are to write a C++ program to produce an inventory repor.pdf
udit652068
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
udit652068
 
Write a java program that would ask the user to enter an input file .pdf
Write a java program that would ask the user to enter an input file .pdfWrite a java program that would ask the user to enter an input file .pdf
Write a java program that would ask the user to enter an input file .pdf
udit652068
 
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdf
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdfUsing the header(dlist) and mainline file (dlistapp) belowYou are .pdf
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdf
udit652068
 
TOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdf
TOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdfTOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdf
TOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdf
udit652068
 
The selection of officials in which of the following branches of the.pdf
The selection of officials in which of the following branches of the.pdfThe selection of officials in which of the following branches of the.pdf
The selection of officials in which of the following branches of the.pdf
udit652068
 

More from udit652068 (20)

Discuss briefly and with examples, What key measures need to be take.pdf
Discuss briefly and with examples, What key measures need to be take.pdfDiscuss briefly and with examples, What key measures need to be take.pdf
Discuss briefly and with examples, What key measures need to be take.pdf
 
Did BP respond in a manner that was appropriate with the oil spill t.pdf
Did BP respond in a manner that was appropriate with the oil spill t.pdfDid BP respond in a manner that was appropriate with the oil spill t.pdf
Did BP respond in a manner that was appropriate with the oil spill t.pdf
 
Describe and provide at least two examples of direct transmission of.pdf
Describe and provide at least two examples of direct transmission of.pdfDescribe and provide at least two examples of direct transmission of.pdf
Describe and provide at least two examples of direct transmission of.pdf
 
Define VRIO in business strategy Define VRIO in business strat.pdf
Define VRIO in business strategy Define VRIO in business strat.pdfDefine VRIO in business strategy Define VRIO in business strat.pdf
Define VRIO in business strategy Define VRIO in business strat.pdf
 
Define e-commerce and describe how it differs from e-business.So.pdf
Define e-commerce and describe how it differs from e-business.So.pdfDefine e-commerce and describe how it differs from e-business.So.pdf
Define e-commerce and describe how it differs from e-business.So.pdf
 
Building Social Business - EssaySolutionBuilding social busine.pdf
Building Social Business - EssaySolutionBuilding social busine.pdfBuilding Social Business - EssaySolutionBuilding social busine.pdf
Building Social Business - EssaySolutionBuilding social busine.pdf
 
You are to write a C++ program to produce an inventory repor.pdf
You are to write a C++ program to produce an inventory repor.pdfYou are to write a C++ program to produce an inventory repor.pdf
You are to write a C++ program to produce an inventory repor.pdf
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
 
Write a java program that would ask the user to enter an input file .pdf
Write a java program that would ask the user to enter an input file .pdfWrite a java program that would ask the user to enter an input file .pdf
Write a java program that would ask the user to enter an input file .pdf
 
Which of the following defines a method doubleEach that returns an ar.pdf
Which of the following defines a method doubleEach that returns an ar.pdfWhich of the following defines a method doubleEach that returns an ar.pdf
Which of the following defines a method doubleEach that returns an ar.pdf
 
When the Fed provides more reserves toprovides more reserves to.pdf
When the Fed provides more reserves toprovides more reserves to.pdfWhen the Fed provides more reserves toprovides more reserves to.pdf
When the Fed provides more reserves toprovides more reserves to.pdf
 
What is a common infrastructure and what does it provideSolutio.pdf
What is a common infrastructure and what does it provideSolutio.pdfWhat is a common infrastructure and what does it provideSolutio.pdf
What is a common infrastructure and what does it provideSolutio.pdf
 
What are the eukaryotic kingdomsQuestion 4 optionsBacteria, Ar.pdf
What are the eukaryotic kingdomsQuestion 4 optionsBacteria, Ar.pdfWhat are the eukaryotic kingdomsQuestion 4 optionsBacteria, Ar.pdf
What are the eukaryotic kingdomsQuestion 4 optionsBacteria, Ar.pdf
 
What are some of the commonly seen WLAN devices What role does each.pdf
What are some of the commonly seen WLAN devices What role does each.pdfWhat are some of the commonly seen WLAN devices What role does each.pdf
What are some of the commonly seen WLAN devices What role does each.pdf
 
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdf
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdfUsing the header(dlist) and mainline file (dlistapp) belowYou are .pdf
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdf
 
Virology.Explain the make up and role of different complexes for .pdf
Virology.Explain the make up and role of different complexes for .pdfVirology.Explain the make up and role of different complexes for .pdf
Virology.Explain the make up and role of different complexes for .pdf
 
TOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdf
TOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdfTOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdf
TOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdf
 
The test solution is made basic and drops of 0.1 M Ca(NO3)2 are adde.pdf
The test solution is made basic and drops of 0.1 M Ca(NO3)2 are adde.pdfThe test solution is made basic and drops of 0.1 M Ca(NO3)2 are adde.pdf
The test solution is made basic and drops of 0.1 M Ca(NO3)2 are adde.pdf
 
The selection of officials in which of the following branches of the.pdf
The selection of officials in which of the following branches of the.pdfThe selection of officials in which of the following branches of the.pdf
The selection of officials in which of the following branches of the.pdf
 
the distance between the N-terminal and C-ternimal amino acids varie.pdf
the distance between the N-terminal and C-ternimal amino acids varie.pdfthe distance between the N-terminal and C-ternimal amino acids varie.pdf
the distance between the N-terminal and C-ternimal amino acids varie.pdf
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 

Recently uploaded (20)

Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 

Please write the class and class with main method clearly!! This is .pdf

  • 1. Please write the class and class with main method clearly!! This is to be written in JAVA. Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables 1-Name: name Access modifier: private Data Type: String 2-Name: currentWeight Access modifier: private Data Type: double 3-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
  • 2. 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
  • 3. 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 } } Solution HI, Please find my implementation. Please let me know in case of any issue. ############### Bag.java ######################## public class Bag { // instance variables private String name; private double currentWeight; private double maximumWeight; // constructor public Bag() { name = ""; currentWeight = 0.0; maximumWeight = 5.0; } public void setName(String newName){ name = newName; } public String getName(){ return name; }
  • 4. public void addItem(double newWeight){ double total = currentWeight + newWeight; if(newWeight > 0 && total <= maximumWeight) currentWeight = total; } public double getCurrentWeight(){ return currentWeight; } public void setMaximumWeight(double newMaximumWeight){ if(newMaximumWeight >0 && (currentWeight <= newMaximumWeight)) maximumWeight = newMaximumWeight; } public double getMaximumWeight(){ return maximumWeight; } } ############### BagDemo.java ############### 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 Scanner sc = new Scanner(System.in); //Create an object of the Bag class refer to this object as myBag Bag myBag = new Bag(); //declare a variable called option of type int int option; //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
  • 5. 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 "); //Save the user’s input into the option variable option = sc.nextInt(); //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 if(option == 1){ System.out.print("Enter name: "); String newName = sc.next(); myBag.setName(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(option == 2){ System.out.print("Enter weight of the item: "); double newWeight = sc.nextDouble(); myBag.addItem(newWeight); } //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(option == 3){ System.out.print("Enter new max weight: "); double newMaximumWeight = sc.nextDouble(); myBag.setMaximumWeight(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
  • 6. else if(option == 4){ System.out.println("Name: "+myBag.getName()); System.out.println("Current Weight: "+myBag.getCurrentWeight()); System.out.println("Max Weight: "+myBag.getMaximumWeight()); } //else if the user picks option 5, display Goodbye. else if(option == 5){ System.out.println("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 else{ System.out.println("Invalid option!!!"); } }while(option != 5); } } /* Sample run: 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 1 Enter name: AmericalTourist 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 2 Enter weight of the item: 34 Press 1 to change the name of the bag Press 2 to add an item to the bag
  • 7. 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 3 Enter new max weight: 50 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 2 Enter weight of the item: 34 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 4 Name: AmericalTourist Current Weight: 34.0 Max Weight: 50.0 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 5 Goodbye */