Question

In: Computer Science

The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...

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.

Solutions

Expert Solution

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


Related Solutions

In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num1; double num2; System.out.print("Enter two integers: "); num1 = console.nextInt(); num2 = console.nextInt(); System.out.println(); if (num1 != 0 && num2 != 0) System.out.printf("%.2f\n", Math.sqrt(Math.abs(num1 + num2 + 0.0))); else if (num1 != 0) System.out.printf("%.2f\n", Math.floor(num1 + 0.0)); else if (num2 != 0) System.out.printf("%.2f\n",Math.ceil(num2 + 0.0)); else System.out.println(0); }} a. What is the...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void main(String[] args) { Rectangle r1 = new Rectangle(0,0,100,150); System.out.println(r1);    Rectangle r2 = new Rectangle(50,75,100,150); System.out.println(r2);    Rectangle r3 = r1.intersection(r2);    } } Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the...
Task 2/2: Java program Based upon the following code: public class Main {   public static void...
Task 2/2: Java program Based upon the following code: public class Main {   public static void main( String[] args ) {     String alphabet = "ABCDEFGHIJKLMNMLKJIHGFEDCBA";     for( <TODO> ; <TODO> ; <TODO> ) {       <TODO>;     } // Closing for loop   } // Closing main() } // Closing class main() Write an appropriate loop definition and in-loop behavior to determine if the alphabet string is a palindrome or not. A palindrome is defined as a string (or more generally, a token) which...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static)...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static) method named firstOne. firstOne accepts a String array and returns a map from Strings to Integer. The map must count the number of passed Strings based on the FIRST letter. For example, with the String array {“banana”, “apples”, “blueberry”, “orange”}, the map should return {“b”:2, “a”:1, “o”:1}. Disregard empty Strings and zero counts. Retrieve first character of String as a char using charAt, or,...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T extends Comparable<T>>    void swap(T[] data, int index1, int index2)    {       T temp = data[index1];       data[index1] = data[index2];       data[index2] = temp;    }    public void sort(T[] data)    {       int position, scan;       for (position = data.length - 1; position >= 0; position--)       {          for (scan = 0; scan <= position - 1; scan++)          {...
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT