Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

CS 212 M5 Assignments 1. Based on the integer List class in the Lecture Notes, write a driver to read 7 integers, and store them into a predefined object myList (shown below), sort the list into...

1 answer below »

CS 212 M5 Assignments

1. Based on the integer List class in the Lecture Notes, write a driver to read 7 integers, and store them into a predefined object myList (shown below), sort the list into ascending order and output the list to the monitor. Then sort the list into descending order, and output the list to the monitor.

List myList = new List();

Hint: refer to #3 of M5 Code Examples.

2. Based on the FractionList class in the Lecture Notes, write a driver to read 5 fraction numbers, and store them into a predefined object fList (shown below), sort the list into ascending order and output the list to the monitor. Then sort the list into descending order, and output the list to the monitor.

FractionList fList = new FractionList();

Hint: refer to #5 of M5 Code Examples.

3. Based on the StrList class in the Lecture Notes, modify it if needed and then write a driver to prompt the following menu and let the user choose an operation and then perform the corresponding operation. Use a loop, so the code could run continuously.

(Bonus of 10 points) To receive the extra credits, the name should be a full name (with or without middle names) and the list should be displayed in a column.

I Insert a name

A Sort the name list to ascending order

D Sort the name list to descending order

F Find if a name on the list

P Print the name list

N Display the size of the name list (Number of names)

Q Quit

Answered Same Day Aug 07, 2021

Solution

Aditya answered on Aug 08 2021
147 Votes
New folde
Fraction.java
New folde
Fraction.java
public class Fraction {
    private int nume, deno;
    private void reduce() 
 reduce to lowest form
    {
        int n = nume;
        int d = deno;
        while (d != 0) {
            int r = n % d;
            n = d;
            d = r;
        }
        if (n != 0) {
            nume /= n;
            deno /= n;
        }
        if (deno < 0) {
            nume *= (-1);
            deno *= (-1);
        }
    }
    public Fraction() {
        nume = 0;
        deno = 1;
    }
    public Fraction(int n, int d) {
        nume = n;
        deno = d;
    }
    public void set(int n, int d) {
        nume = n;
        deno = d;
    }
    public Fraction add(Fraction x) 
 this + x ==> add
    {
        Fraction temp = new Fraction(this.nume * x.deno + this.deno * x.nume, this.deno * x.deno);
        temp.reduce();
        return temp;
    }
    public Fraction sub(Fraction x) 
 this - x ==> su
    {
        Fraction temp = new Fraction(this.nume * x.deno + this.deno * x.nume, this.deno * x.deno);
        temp.reduce();
        return temp;
    }
    public Fraction mult(Fraction x) {
        Fraction temp = new Fraction(nume * x.nume, deno * x.deno);
        temp.reduce();
        return temp;
    }
    public Fraction div(Fraction x) {
        Fraction temp = new Fraction(nume * x.deno, deno * x.nume);
        temp.reduce();
        return temp;
    }
    public void writeOut() {
        reduce();
        System.out.print(nume + "/" + deno);
    }
    public String toString() {
        reduce();
        return (nume + "/" + deno);
    }
    public boolean equals(Fraction x) {
        return nume * x.deno == deno * x.nume;
    }
    public boolean greaterThan(Fraction x) {
        return nume * x.deno > deno * x.nume;
    }
    public boolean lessThan(Fraction x) {
        return nume * x.deno < deno * x.nume;
    }
}
New folde
fractionDriver.java
New folde
fractionDriver.java
import java.util.Scanner;
public class fractionDriver {
    private static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        FractionList z = new FractionList();
        Fraction zero = new Fraction();
        Fraction item;
        int count =0;
        while (count <5) {
            item = aFrac();
            if (item.lessThan(zero))
            {
                System.out.println("Enter a valid number");
            }
            else 
            {
                z.Insert(item);
                count ++;
            }
            
        }
        System.out.println("List in ascending order – ");
        z.selectionSort();
        z.Print();
        System.out.println("\nList in ascending order – ");
        z.descendingSort();
        z.Print();
        System.out.println();
    }
    private static Fraction aFrac() {
        System.out.print("Enter number: ");
        String str = input.next();
        String[] parts = str.split("/", 2);
        int n = Integer.parseInt(parts[0]);
        int d = Integer.parseInt(parts[1]);
        Fraction frac = new Fraction(n, d);
        return frac;
    }
}
New folde
FractionList.java
New...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here