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...
1, Create a class Car. The class has four instance fields: make (String - admissible values:...
1, Create a class Car. The class has four instance fields: make (String - admissible values: Chevy, Ford, Toyota, Nissan, Hyundai) size (String - admissible values: compact, intermediate, fullSized), weight (int - admissible values: 500 <= weight <= 4000) horsePower (int - admissible values: 30 <= horsePower <= 400) Provide a default constructor that sets String values to null and int values to 0 a parameterized constructor that sets all four instance fields to the parameter values setters and getters...
java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and...
java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and address (String). Implement the initialization constructor . Implement the setters and getters for all attributes. Implement the toString() method to display all attributes. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. Implement the compareTo (Entry other) method that returns 0 if the two...
Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String),...
Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String), phoneNumber (String), and address (String). 2. Implement the initialization constructor . 3. Implement the setters and getters for all attributes. 4. Implement the toString() method to display all attributes. 5. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. 6. Implement the compareTo (Entry...
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...
1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account...
1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account Number and Account Balance. There are also other variables called MIN_BALANCE=9.99, REWARDS_AMOUNT=1000.00, REWARDS_RATE=0.04. They look like constants, but for now, they are variables of type double Here is the UML for the class:                                                         BankAccount -string accountName // First and Last name of Account holder -int accountNumber // integer -double accountBalance // current balance amount + BankAccount()                     //default constructor that sets name to “”,...
Design a ship class that has the following data fields: A data field for the name...
Design a ship class that has the following data fields: A data field for the name of the ship (a string). A data field for the year that the ship was built (a String). A constructor and appropriate accessors and mutators. A toString method that displays the ship’s name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following member: A field for the maximum number of passengers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT