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...

  • Create a class called CarNode which has fields for the data (a Car) and next (CarNode)...

    Create a class called CarNode which has fields for the data (a Car) and next (CarNode) instance variables. Include a one-argument constructor which takes a Car as a parameter. (For hints, see the PowerPoint on "Static vs. Dynamic Structures”.) public CarNode (Car 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 CarList. This should be a...

  • 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...

  • Language: C++ (Please show output) Part 1 - LIST Create an unsorted LIST class. Each list...

    Language: C++ (Please show output) Part 1 - LIST Create an unsorted LIST class. Each list should be able to store 100 names. Part 2 - Create a Class ArrayListClass It will contain an array of 27 "list" classes. Next, create a Class in which is composed a array of 27 list classes. Ignore index 0... Indexes 1...26 correspond to the first letter of a Last name. Again - ignore index 0. index 1 is names starting with A, index...

  • 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...

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