Question

Implement the following classes in the UML: 1. List class includes:       constructor List(): Create an...

Implement the following classes in the UML:

1. List class includes:

      constructor List(): Create an empty list with a length of 100. Hint: double [] list = new double [100];

      constructor List(len: int): Create a List with a user specified maximum length. Hint: double [] list = new double [len];

      add: add a new element to the end of the unsorted list.

      print: display the list

2.SortedList class includes

      two constructors : similar to the constructors in List,  

add: add a new element into the ascending sorted list.

3.TestProgram: main method creates a genera list (unsorted) and a sorted list with random numbers using add methods; display the unsorted and sorted lists.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class List {
    private double [] list;
    private int size = 0;

    public List() {
        list = new double [100];
    }
    public List(int len) {
        list = new double [len];
    }
    public void add(double item) {
        if(size == list.length) {
            return;
        }
        list[size++] = item;
    }
    public void print() {
        for(int i=0; i<size; i++) {
            System.out.print(list[i] + " ");
        }
        System.out.println();
    }

    public boolean isEmpty() {
        return size == 0;
    }
}

class SortedList {
    private double [] list;
    private int size = 0;

    public SortedList() {
        list = new double [100];
    }
    public SortedList(int len) {
        list = new double [len];
    }

    /* Checks if the array is empty */
    public boolean isEmpty() {
        return size == 0;
    }

    public void add(double x) {
        if (list.length == size) {
            return;
        }

        list[size] = x;
        int i = size - 1;
        while(i >= 0 && x < list[i]) {
            list[i+1] = list[i];
            i--;
        }
        list[i+1] = x;              
        size++;
    }

    public void print() {
        for(int i=0; i<size; i++) {
            System.out.print(list[i] + " ");
        }
        System.out.println();
    }
}
public class ListDemo {
    public static void main(String args[]) {
        List list = new List();
        SortedList slist = new SortedList();

        for(int i=0; i<10; i++) {
            int x = (int) (Math.random() * 10);
            list.add(x);
            slist.add(x);
        }

        list.print();
        slist.print();

    }
}


Hi. please find the answer above.. i have given comments so that it is very easy for you to understand the flow. In case of any doubts, please ask in comments. If the answer helps you, please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
Implement the following classes in the UML: 1. List class includes:       constructor List(): Create an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • List of Candles Create a class called CandleNode which has fields for the data (a Candle)...

    List of Candles Create a class called CandleNode which has fields for the data (a Candle) and next (CandleNode) instance variables. Include a one-argument constructor which takes a Candle as a parameter. (For hints, see the PowerPoint on "Static vs. Dynamic Structures”.) public CandleNode (Candle c) { . . } The instance variables should have protected access. There will not be any get and set methods for the two instance variables. Create an abstract linked list class called CandleList. This...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • Java Create a class NumberList that stores a list of integers and a list of doubles....

    Java Create a class NumberList that stores a list of integers and a list of doubles. Create a driver class that creates two objects of type NumberList and and ask the user to fill one of these lists. The other list should have integer values 1,2,3,4 and double values 1.1, 2.2, 3.3, 4.4. Declare a winner between the two objects by comparing their integer lists element by element and counting which list has more winning positions (the lists will need...

  • using java Develop the classes and interface represented in the following UML diagram: <cinterface >> Passenger...

    using java Develop the classes and interface represented in the following UML diagram: <cinterface >> Passenger 1 String name has passengers String getNamel) String getFaro Typel) String getName() void display Passengers Person String frame String last Name intage Note that the Person class includes both a first and last name, but the Passenger interface requires a full name. The field fare Type is determined by a passenger's age: "Regular fare is the full fare, "Youth" is a reduced fare for...

  • Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in thr...

    Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in three dimensions labeled x, y and z. You will then use the class to perform some calculations on an array of these points. You need to draw a UML diagram for the class (Point3D) and then implement the class The Point3D class will have the...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • In Java Create an interface called Amount that includes a method called setPrice (). Create an a...

    In Java Create an interface called Amount that includes a method called setPrice (). Create an abstract class named Book that inherits from the interface Amount. Include a String field for the book’s title and a double field for the book’s Price . Within the class, include a constructor that requires the book title and add two getter methods — one that returns the title and one that returns the price. Include an abstract method named setPrice (). Create a...

  • Create a linked list with the following features. A Node class that stores the data type...

    Create a linked list with the following features. A Node class that stores the data type of your choice. A constructor that creates a dummy header node. void display() -- A method for printing the list. void add(item) -- A method for adding a value to the beginning of the list void addEnd(item) -- A method of adding a value to the end of the list. bool contains(item) -- A method for finding a specified value in the list. int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT