Question

In: Computer Science

JAVAFX LAB 2 1. The Soda class has two fields: a String for the name and...

JAVAFX LAB 2

1. The Soda class has two fields: a String for the name and a double for the price.

2. The Soda class has two constructors. The first is a parameterized constructor that takes a String and a double to be assigned to the fields of the class. The second is a copy constructor that takes a Soda object and assigns the name and price of that object to the newly constructed Soda object.

3. The Soda class has two getters: one to return the name and one to return the price.

4. The VendingMachine has one field for a Soda. This can initially reference a null object by assigning null to the field.

5. The VendingMachine has one constructor that takes a Soda. Remember to assign a new Soda to the Soda field securely.

6. The addSoda method of the VendingMachine class takes a Soda. Check if the field of the class references a null object (you can use the equals operator to check what the variable references). If the field references a null object, assign the new Soda to the Soda field securely and return true. If the field does not reference a null object, just return false.

7. The removeSoda method of the VendingMachine class takes no arguments. Check if the field of the class does not reference a null object (you can use the equals operator to check what the variable references). If the field does not reference a null object, assign null to the Soda field and return true. If the field references a null object, just return false. 8. In the VendingMachineDemo, change the name and price for the Soda being added to the VendingMachine to any other name and price of your choosing.

Solutions

Expert Solution


public class VendingMachineDemo {
        public static void main(String[] args) {
                Soda soda = new Soda("Coke",10.0);
                VendingMachine machine = new VendingMachine(soda);
                System.out.println("Vending machine currently has ::"+ machine.getSoda());
                
                machine.getSoda().setName("Sprite");
                machine.getSoda().setPrice(20.0);
                
                System.out.println("After renaming Vending machine currently has ::"+ machine.getSoda());
        }
}

class Soda {
        private String name;
        private double price;
        
        public Soda(String name, double price) {
                this.name = name;
                this.price = price;
        }
        
        public Soda(Soda source) {
                this.name = source.name;
                this.price = source.price;
        }

        public String getName() {
                return name;
        }

        public double getPrice() {
                return price;
        }

        public void setName(String name) {
                this.name = name;
        }

        public void setPrice(double price) {
                this.price = price;
        }

        @Override
        public String toString() {
                return "Soda [name=" + name + ", price=" + price + "]";
        }
}

class VendingMachine {
        private Soda soda;
        
        
        public Soda getSoda() {
                return soda;
        }

        public void setSoda(Soda soda) {
                this.soda = soda;
        }

        VendingMachine(Soda sourceSoda) {
                if(this.soda == null) {
                        soda = new Soda(sourceSoda.name,sourceSoda.price);
                }
        }
        
        public boolean addSoda(Soda soda) {
                if(this.soda == null) {
                        soda = new Soda(soda.name,soda.price);
                        return true;
                } else {
                        return false;
                }
        }
        
        public boolean removeSoda() {
                if(this.soda != null) {
                        this.soda = null;
                        return true;
                } else {
                        return false;
                }
        }
}

Here first I created a soda object with soda name as "Coke" and price is 10.0

After that I have updated the soda object with values.

Now new name is "Sprite" and price is 20.0.


Related Solutions

1. The Item class includes two fields: a String for the name and a double for the price of an item that will be added to the inventory.
  1. The Item class includes two fields: a String for the name and a double for the price of an item that will be added to the inventory. 2. The Item class will require one constructor that takes a name and price to initialize the fields. Since the name and price will be provided through TextFields, the parameters can be two Strings (be sure to convert the price to a double before storing this into the field). 3. Write...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's name • idNumber: an int representing the employee's ID number • rate: a double containing the employee's hourly pay rate • hours: an int representing the number of hours this employee has worked The class should also have the following methods: • Constructor: takes the employee's name and ID number as arguments • Accessors: allow access to all of the fields of the Payroll...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
This lab uses a Student class with the following fields: private final String firstName; private final...
This lab uses a Student class with the following fields: private final String firstName; private final String lastName; private final String major; private final int zipcode; private final String studentID; private final double gpa; A TestData class has been provided that contains a createStudents() method that returns an array of populated Student objects. Assignmen The Main method prints the list of Students sorted by last name. It uses the Arrays.sort() method and an anonymous Comparator object to sort the array...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
Create a struct MenuItem containing fields for name (a string) and price (a float) of a...
Create a struct MenuItem containing fields for name (a string) and price (a float) of a menu item for a diner. Create a ReadItem() function that takes an istream and a MenuItem (both by reference) and prompts the user for the fields of the MenuItem, loading the values into the struct's fields. Create a PrintItem() function that takes an output stream (by reference) and a MenuItem (by value) and prints the MenuItem fields to the stream in a reasonable one-line...
Write a Data Element Class named Property that has fields tohold the property name, the...
Write a Data Element Class named Property that has fields to hold the property name, the city where the property is located, the rent amount, the owner's name, and the Plot to be occupied by the property, along with getters and setters to access and set these fields. Write a parameterized constructor (i.e., takes values for the fields as parameters) and a copy constructor (takes a Property object as the parameter). Follow the Javadoc file provided.Write a Data Element Class...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
Write a class named ContactEntry that has fields for a person’s name, phone number and email...
Write a class named ContactEntry that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five ContactEntry objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. I repeat, NO-ARG constructors....
Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT