In: Computer Science
The following Java program is NOT designed using class/object concept.
public class demo_Program4_non_OOP_design {
public static void main(String[] args) {
String bottle1_label="Milk";
float bottle1_volume=250;
float bottle1_capacity=500;
bottle1_volume=addVolume(bottle1_label,
bottle1_volume,bottle1_capacity,200);
System.out.println("bottle label: " + bottle1_label +
", volume: " + bottle1_volume +
", capacity: " +bottle1_capacity);
String bottle2_label="Water";
float bottle2_volume=100;
float bottle2_capacity=250;
bottle2_volume=addVolume(bottle2_label,
bottle2_volume,bottle2_capacity,500);
System.out.println("bottle label: " + bottle2_label +
", volume: " + bottle2_volume +
", capacity: " +bottle2_capacity);
}
public static float addVolume(String label, float bottleVolume,
float capacity, float addVolume) {
if (bottleVolume + addVolume <= capacity)
{
bottleVolume = bottleVolume + addVolume;
}
else
{
bottleVolume = capacity;
System.out.println(label + " bottle is overfilled.");
}
return bottleVolume;
}
}
(Thinking in objects) Redesign the above Java program to meet the
following requirements:
(1) Design a bottle class with
a. necessary data fields, constructors and methods.
b. toString() method is required for this bottle class.
(2) Write a test class(xxxx_Program4) to include a main() method
that does the following steps in
order:
a. Create 1st bottle object with -- label: Milk, volume: 250,
capacity: 500
b. Add volume 200 to 1st bottle object.
c. Print the 1st object’s information by calling toString()
method.
d. Create 2nd bottle object with -- label: Water, volume: 100,
capacity: 250
e. Add volume 500 to 2ndbottle object.
f. Print the 2nd object’s information by calling toString()
method.
(3) The output should be like:
bottle label: Milk, volume: 450.0, capacity: 500.0
Water bottle is overfilled.
bottle label: Water, volume: 250.0, capacity: 250.0
(4) This Program assignment does not take any input from the keyboard.
The solution to the above problem in Java is as follows:-
Code-
//Declaration of class Bottle
class Bottle{
String label;
float volume;
float capacity;
//Constructor to initialize Bottle objects
public Bottle(String l,float vol,float cap){
label=l;
volume=vol;
capacity=cap;
}
//Add volume to bottle
public void addVolume(float vol){
if(volume+vol<=capacity)
volume=volume+vol;
else{
volume=capacity;
System.out.println(label+" bottle is overfilled");
}
}
//Convert details of bottle to String output
public String toString(){
String output="Bottle label:
"+label+", Capacity: "+capacity+", Volume: "+volume;
return output;
}
}
public class test_program4{
public static void main(String[] args){
Bottle b1=new
Bottle("Milk",250,500); //Create 1st bottle object with -- label:
Milk, volume: 250, capacity: 500
b1.addVolume(200); // Add volume
200 to 1st bottle object.
System.out.println(b1.toString());//Print the 1st object’s
information by calling toString() method.
Bottle b2=new
Bottle("Water",100,250); //Create 2nd bottle object with -- label:
Water, volume: 100, capacity: 250
b2.addVolume(500); // Add volume
500 to 2ndbottle object.
System.out.println(b2.toString());//Print the 2nd object’s
information by calling toString() method.
}
}
Code Screenshots-
Outputs-
Feel free to comment for any issues, do rate the answer positively