Question

In: Computer Science

Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods....

Code in Java

1. Create a class Flower with data: Name, Price, Color and properly methods.

2. Create another class named ListFlower. This class manages a collection of Flower (may be LinkedList) named a. Implementing some methods for ListFlower:

  • Add: add new item of Flower to a
  • Display: display all items of a
  • sort(): sort as descending by Price and display all items of a
  • search(Flower f): check and return whether f is exists in a or not.
  • delete(int pos): remove the item at the position pos of a.

Write a program to use ListFlower as above menu.

Make your own main program to test all above methods.

Solutions

Expert Solution

I have implemented "Flower" class and "ListFlower" Class which will add, delete , display, search and sort the flowers by price in descending order.

Here, ListFlower class is work as a singular linkedlist.

Flower Class:-

// This class represents Flower 
class Flower{
    
    // store flower name
    String name;
    
    // store flower price
    double price;
    
    // store flower color
    String color;
    
    // store next flower
    Flower next;
    
    
    // initialize constructor for the flower data members
    public Flower(String name, double price, String color) {
        this.name = name;
        this.price = price;
        this.color = color;
        
        this.next = null;
    }

    
    /*
    getter and setter methods
     */
    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Flower getNext() {
        return next;
    }

    public void setNext(Flower next) {
        this.next = next;
    }
    
}

ListFlower Class:-

class ListFlower{
    
    // store fist flower in the linkedlist of flower
    Flower head;
    
    /**
     * Add flower to the flower list
     * @param f to be added
     */
    public void add(Flower f){
        
        
        // check whether list is empty or not?
        if(head == null){
            
            // make flower as head
            head = f;
            
            return;
        }
        
        // now add the flower at the end of the flower list
        Flower flower = head;
        
        // reach to the last flower
        while(flower.next != null){
            
            // goto the next flower
            flower = flower.getNext();
        }
        
        // store new flower to the next of last flower
        flower.setNext(f);
    }
    
    
    /**
     * This method prints all the flowers from the flower list.
     */
    public void display(){
        
        // check whether list is empty or not?
        if(head == null){
            
            System.out.println("No Flower in the list!");
            
            return;
        }
        
        // store head
        Flower curr = head;
        
        while(curr != null){
            
            System.out.println("\nName : "+curr.getName());
            System.out.println("Price : "+curr.getPrice());
            System.out.println("Color : "+curr.getColor());
            // goto the next flower
            
            // if we print the last flower then dont print next flower line
            if(curr.next != null)
                System.out.println("\n---------Next Flower---------");
            
            curr = curr.getNext();
        }
    }
    
    
    /**
     * This method sorts the flowers in descending order by pice
     */
    public void sort(){
        
        // if list is empty
        if(head == null){
            System.out.println("No FLower in the list!");
            return;
        }
        
        // sort the list using selection sort in descending oreder based on flower price
        for(Flower first = head; first != null; first = first.getNext()){
            
            for(Flower second = first.getNext(); second != null; second = second.getNext()){
                
                // compare flowers price
                if(first.getPrice() < second.getPrice()){
                    
                    // swap the flowers name
                    String tempName = first.getName();
                    first.setName(second.getName());
                    second.setName(tempName);
                    
                    // swap the flower price
                    double tempPrice = first.getPrice();
                    first.setPrice(second.getPrice());
                    second.setPrice(tempPrice);
                    
                    
                    // swap the flowers color
                    String tempColor = first.getColor();
                    first.setColor(second.getColor());
                    second.setColor(tempColor);
                    
                }
                
            }
            
        }
        System.out.println("Flowers sorted in descending order based on price");
    }
    
    
    /**
     * This method search flower in the flower list
     * @param f to be searched
     * @return true if flower(f) is exist
     */
    public boolean search(Flower f){
        
        // if list is empty then retur false
        if(head == null){
            return false;
        }
        
        // store head
        Flower curr = head;
        
        // compare each flower equal or not?
        while(curr != null){
            
            // compare each flower value
            if(curr.getName().equals(f.getName()) && curr.getPrice() == f.getPrice() && curr.getColor().equals(f.getColor())){
                // here flowers data is equal then return true;
                return true;
            }
            
            // goto the next flower
            curr = curr.getNext();
        }
        
        // otherwise return flase
        return false;
    }
    
    
    /**
     * This method deleter flower from the flower list
     * @param pos of flower which is deleted
     */
    public void delete(int pos){
        
        // check whether list is empty or not?
        if(head == null){
            System.out.println("No flower in the list!");
            return;
        }
        
        /*
            Case 1: if position is one then remove first flower
                    from the list
        */
        if(pos == 1){
            
            // store next of head into the head
            head = head.getNext();
            System.out.println(pos+" Flower is deleted");
            return;
        }
        
        
        // store head 
        Flower curr = head;
        
        // store prev flower for deletion purpose
        Flower prev = curr;
        int i = 1;
        
        // searh position
        while(curr != null){
            
            // if position is match then remove curr flower from that positiion
            if(i == pos){
                
                // now store the next of curr to the next of prev
                prev.setNext(curr.getNext());
                
                System.out.println(pos+" Flower is deleted");
                return;
            }
            
            // incerement i
            i++;
            
            // store prev of curr
            prev = curr;
            
            // goto the next of flower
            curr = curr.getNext();
        }
        System.out.println("No flower is found in "+pos+" postion");
    }
    
}

In ListFlower class, delete(int pos) method remove the flower from the list and here, postion value is start from the 1 for the flower linkedlist.

I have attached a java program which will provide menu to the user by which user can add, delete, sort, search and display the flowers from the flower list and program will be ended when the user enter "6" for the exit.

TestFlowerList.java file:-

import java.util.Scanner;

// This class represents Flower 
class Flower{
    
    // store flower name
    String name;
    
    // store flower price
    double price;
    
    // store flower color
    String color;
    
    // store next flower
    Flower next;
    
    
    // initialize constructor for the flower data members
    public Flower(String name, double price, String color) {
        this.name = name;
        this.price = price;
        this.color = color;
        
        this.next = null;
    }

    
    /*
    getter and setter methods
     */
    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Flower getNext() {
        return next;
    }

    public void setNext(Flower next) {
        this.next = next;
    }
    
}


/*
    This class represents flower list
    using singular linked list
*/
class ListFlower{
    
    // store fist flower in the linkedlist of flower
    Flower head;
    
    /**
     * Add flower to the flower list
     * @param f to be added
     */
    public void add(Flower f){
        
        
        // check whether list is empty or not?
        if(head == null){
            
            // make flower as head
            head = f;
            
            return;
        }
        
        // now add the flower at the end of the flower list
        Flower flower = head;
        
        // reach to the last flower
        while(flower.next != null){
            
            // goto the next flower
            flower = flower.getNext();
        }
        
        // store new flower to the next of last flower
        flower.setNext(f);
    }
    
    
    /**
     * This method prints all the flowers from the flower list.
     */
    public void display(){
        
        // check whether list is empty or not?
        if(head == null){
            
            System.out.println("No Flower in the list!");
            
            return;
        }
        
        // store head
        Flower curr = head;
        
        while(curr != null){
            
            System.out.println("\nName : "+curr.getName());
            System.out.println("Price : "+curr.getPrice());
            System.out.println("Color : "+curr.getColor());
            // goto the next flower
            
            // if we print the last flower then dont print next flower line
            if(curr.next != null)
                System.out.println("\n---------Next Flower---------");
            
            curr = curr.getNext();
        }
    }
    
    
    /**
     * This method sorts the flowers in descending order by pice
     */
    public void sort(){
        
        // if list is empty
        if(head == null){
            System.out.println("No FLower in the list!");
            return;
        }
        
        // sort the list using selection sort in descending oreder based on flower price
        for(Flower first = head; first != null; first = first.getNext()){
            
            for(Flower second = first.getNext(); second != null; second = second.getNext()){
                
                // compare flowers price
                if(first.getPrice() < second.getPrice()){
                    
                    // swap the flowers name
                    String tempName = first.getName();
                    first.setName(second.getName());
                    second.setName(tempName);
                    
                    // swap the flower price
                    double tempPrice = first.getPrice();
                    first.setPrice(second.getPrice());
                    second.setPrice(tempPrice);
                    
                    
                    // swap the flowers color
                    String tempColor = first.getColor();
                    first.setColor(second.getColor());
                    second.setColor(tempColor);
                    
                }
                
            }
            
        }
        System.out.println("Flowers sorted in descending order based on price");
    }
    
    
    /**
     * This method search flower in the flower list
     * @param f to be searched
     * @return true if flower(f) is exist
     */
    public boolean search(Flower f){
        
        // if list is empty then retur false
        if(head == null){
            return false;
        }
        
        // store head
        Flower curr = head;
        
        // compare each flower equal or not?
        while(curr != null){
            
            // compare each flower value
            if(curr.getName().equals(f.getName()) && curr.getPrice() == f.getPrice() && curr.getColor().equals(f.getColor())){
                // here flowers data is equal then return true;
                return true;
            }
            
            // goto the next flower
            curr = curr.getNext();
        }
        
        // otherwise return flase
        return false;
    }
    
    
    /**
     * This method deleter flower from the flower list
     * @param pos of flower which is deleted
     */
    public void delete(int pos){
        
        // check whether list is empty or not?
        if(head == null){
            System.out.println("No flower in the list!");
            return;
        }
        
        /*
            Case 1: if position is one then remove first flower
                    from the list
        */
        if(pos == 1){
            
            // store next of head into the head
            head = head.getNext();
            System.out.println(pos+" Flower is deleted");
            return;
        }
        
        
        // store head 
        Flower curr = head;
        
        // store prev flower for deletion purpose
        Flower prev = curr;
        int i = 1;
        
        // searh position
        while(curr != null){
            
            // if position is match then remove curr flower from that positiion
            if(i == pos){
                
                // now store the next of curr to the next of prev
                prev.setNext(curr.getNext());
                
                System.out.println(pos+" Flower is deleted");
                return;
            }
            
            // incerement i
            i++;
            
            // store prev of curr
            prev = curr;
            
            // goto the next of flower
            curr = curr.getNext();
        }
        System.out.println("No flower is found in "+pos+" postion");
    }
    
}


/*
    This class provide menu for the user by which
    user can perforem operations on the ListFlower class
*/
public class TestFlowerList {
    
    public static void main(String[] args) {
        
        // create an object of ListFlower class
        ListFlower listFlower = new ListFlower();
        
        // create an object of scanner class for user input
        Scanner sc = new Scanner(System.in);
        
        int ch = 0;
        
        // create menu for the flower list
        do{
            System.out.println("\n1. Add flower");
            System.out.println("2. Display all flowers");
            System.out.println("3. Sort flower list by price in descending order");
            System.out.println("4. Search flower");
            System.out.println("5. Delete flower");
            System.out.println("6. Exit");
            
            // get the choice from the user
            System.out.print("Enter Choice ::");
            ch = sc.nextInt();
            
            System.out.println();
            
            sc.nextLine();
            
            switch(ch){
                
                case 1:
                        // get the flower name from the user
                        System.out.print("Enter flower name::");
                        String name = sc.nextLine();
                        
                        // get the flower price from the user
                        System.out.print("Enter flower price::");
                        double price = sc.nextDouble();
                        
                        sc.nextLine();
                        
                        // get the flower color from the user
                        System.out.print("Enter flower color::");
                        String color = sc.nextLine();
                        
                        // create flower using flower class object
                        Flower newFlower = new Flower(name, price, color);
                        
                        // add the flower by calling add() method
                        listFlower.add(newFlower);
                        
                        
                        break;
                        
                case 2:
                        // call display() method for diplaying flowers
                        listFlower.display();
                        
                        break;
                
                case 3:
                        // sort the flower list by price using sort() method in descending order
                        listFlower.sort();
                        
                        break;
                        
                case 4:
                        // get the flower name from the user
                        System.out.print("Enter searched flower name::");
                        String searchedName = sc.nextLine();
                        
                        // get the flower price from the user
                        System.out.print("Enter searched flower price::");
                        double searchedPrice = sc.nextDouble();
                        
                        sc.nextLine();
                        
                        // get the flower color from the user
                        System.out.print("Enter searched flower color::");
                        String searchedColor = sc.nextLine();
                        
                        // create searched object of flower class
                        Flower searchedFlower = new Flower(searchedName, searchedPrice, searchedColor);
                        
                        // call the search() method for searched flower
                        if(listFlower.search(searchedFlower)){
                            
                            System.out.println("Searched flower is in the flower list");
                        }else{
                            
                            System.out.println("Searched flower is not in the flower list");
                        }
                        
                        break;
                        
                case 5:
                    
                        // get the deleted flower position from the user
                        System.out.print("Enter positio for deleteion ::");
                        int pos = sc.nextInt();
                        
                        // call the delete() method
                        listFlower.delete(pos);
                        
                        break;
                        
                case 6:
                        System.out.println("---------------Exit---------------");
                        break;
                        
                default:
                        System.out.println("Invalid choice");
            }
        }while(ch != 6);
    }
}

Ouput:-

1> Add flowers detail when user choice "1".

2> Display flowers when user choice "2".

3> Sort flowers when user choice "3".

4> Search flower when user choice "4".

5> delete flower when user choice "5".

6> End of the program when user choose "6".

I hope you will understand the above program.

Do you feel needful and useful then please upvote me.

Thank you.


Related Solutions

1. Create a class Car with data: Name, Price, Production and properly methods. 2. Create another...
1. Create a class Car with data: Name, Price, Production and properly methods. 2. Create another class named GenericCar with a parameter of the T type. This class manages a collection of object T (may be LinkedList) named a. Implementing some methods for GenericCar: Add: add new item of T to a Display: display all items of a getSize: return the number item of a checkEmpty: check and return whether a is empty or not delete(int pos): remove the item...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods designed to perform simple conversions. Specifically, you will be writing methods to convert temperature between Fahrenheit and Celsius and length between meters and inches and practicing overloading methods. See the API document for the Conversion class for a list of the methods you will write. Also, because all of the methods of the Conversion class will be static, you should ensure that it is...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for the following: 1. Name the class SalonServices 2. Add private data fields: a. salonServiceDescription – This field is a String type b. price - This field is a double type 3. Create two methods that will set the field values. a. The first method setSalonServiceDescription() accepts a String parameter defined as service and assigns it to the salonServiceDescription. The method is not static b....
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
This code in java: Create a class named car. A car has color, model, company, registration...
This code in java: Create a class named car. A car has color, model, company, registration number. You can stear a car. A car can move forward. A car has a gear box. A typical gear decide weather car is moving forward or backward. A person owns a car. Kindly add some other functionalities like refuel
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color....
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color to “white”; this is called a default constructor. Next, create a second constructor that takes in two parameters. The second constructor should assign those parameters to the attributes. Then, in main, create two Rabbit objects. For the first Rabbit object, call the first constructor. For the second...
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT