Question

In: Computer Science

IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user...

IN JAVA PLEASE ASAP !!!

I just need the main and mergesort function

  1. Ask the user for the number of elements, not to exceed arraySize = 20 (put appropriate input validation)
  2. Ask the user for the type of data they will enter - EnglishGrade or MathGrade objects.
  3. Use your EnglishGrade and MathGrade classes
  4. Based on the input, create an appropriate array for the data to be entered.
  5. Write a helper function called recursionMergeSort such that:
    • It is a standalone function not part of any Grade class
    • Takes in the same type of parameters as any standard MergeSort with recursion behavior, i.e.
      • void MergeSort(EnglishGrade arr[], int size)
    • Prints out how the array looks every time a recursive step returns back to its caller
    • You might need to pass in a third parameter which identifies the array to be printed - this is language dependent.
  6. In your main, allow the user to enter data of their own choosing up to their chosen array size.
  7. Then sort the array using your sort function (recursionMergeSort)
  8. In your main, make sure that the output is being written to console as well as an output file at the same time.

EnglishGrade class:

public class EnglishGrade {

private int firstEnglishTestGrade;

private int secondEnglishTestGrade;

public EnglishGrade(int firstEnglishTestGrade, int secondEnglishTestGrade) {

super(firstEnglishTestGrade, secondEnglishTestGrade);

}

public EnglishGrade() {

super();

this.secondEnglishTestGrade = secondEnglishTestGrade;

this.firstEnglishTestGrade = firstEnglishTestGrade;

}

//getters

public int getfirstEnglishGrade() {

return firstEnglishTestGrade;

}

public int getsecondEnglishGrade() {

return secondEnglishTestGrade;

}

//setters

public void setfirstEnglishGrade(int firstEnglishTestGrade) {

this.firstEnglishTestGrade = firstEnglishTestGrade;

}

public void setsecondEnglishGrade(int secondEnglishTestGrade) {

this.secondEnglishTestGrade = secondEnglishTestGrade;

}

MathGrade Class:

public class MathGrade extends EnglishGrade {

private int firstMathTestGrade;

private int secondMathTestGrade;

public MathGrade(int firstMathTestGrade, int secondMathTestGrade) {

super(firstMathTestGrade, secondMathTestGrade);

}

public MathGrade() {

super();

this.secondMathTestGrade = secondMathTestGrade;

this.firstMathTestGrade = firstMathTestGrade;

}

//getters

public int getfirstMathGrade() {

return firstMathTestGrade;

}

public int getsecondMathGrade() {

return secondtMathTestGrade;

}

//setters

public void setfirstMathGrade(int firstMathTestGrade) {

this.firstMathTestGrade = firstMathTestGrade;

}

public void setsecondMathGrade(int secondtMathTestGrade) {

this.secondtMathTestGrade = secondtMathTestGrade;

}

Solutions

Expert Solution

Hello! :)

The code you gave didn't compile so I had to make some changes.

Here are the codes to solve the assignment:

EnglishGrade.java:

public class EnglishGrade {
    private int firstEnglishTestGrade;
    private int secondEnglishTestGrade;

    public EnglishGrade(int firstEnglishTestGrade, int secondEnglishTestGrade) {
        this.secondEnglishTestGrade = secondEnglishTestGrade;
        this.firstEnglishTestGrade = firstEnglishTestGrade;
    }

    public EnglishGrade() {
    }

    //getters
    public int getfirstEnglishGrade() {
        return firstEnglishTestGrade;
    }

    public int getsecondEnglishGrade() {
        return secondEnglishTestGrade;
    }

    //setters
    public void setfirstEnglishGrade(int firstEnglishTestGrade) {
        this.firstEnglishTestGrade = firstEnglishTestGrade;
    }

    public void setsecondEnglishGrade(int secondEnglishTestGrade) {
        this.secondEnglishTestGrade = secondEnglishTestGrade;
    }
}

MathGrade.java:

public class MathGrade extends EnglishGrade {
    private int firstMathTestGrade;
    private int secondMathTestGrade;

    public MathGrade(int firstMathTestGrade, int secondMathTestGrade) {
        super(firstMathTestGrade, secondMathTestGrade);
        this.secondMathTestGrade = secondMathTestGrade;
        this.firstMathTestGrade = firstMathTestGrade;
    }

    public MathGrade() {
        super();
    }

    //getters
    public int getfirstMathGrade() {
        return firstMathTestGrade;
    }

    public int getsecondMathGrade() {
        return secondMathTestGrade;
    }

    //setters
    public void setfirstMathGrade(int firstMathTestGrade) {
        this.firstMathTestGrade = firstMathTestGrade;
    }

    public void setsecondMathGrade(int secondtMathTestGrade) {
        this.secondMathTestGrade = secondtMathTestGrade;
    }
}

Main.java:

import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;

import java.io.File;
import java.io.FileOutputStream;

import java.io.IOException;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        // creating buffers
        br = new BufferedReader(new InputStreamReader(System.in));
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("output.txt"))));

        // reading arraySize
        System.out.print("Enter the number of elements: ");
        int arraySize = Integer.parseInt(br.readLine());

        // validating arraySize
        if(arraySize > 20)
        {
            System.out.println("Error: You exceeded arraySize 20.");
            System.exit(1);
        }

        // reading type of data
        System.out.println("Types of data: ");
        System.out.println("1. EnglishGrade");
        System.out.println("2. MathGrade");
        System.out.print("Enter the type of data you want to enter: ");
        typeOfData = Integer.parseInt(br.readLine());

        // validating typeOfData
        if(typeOfData != 1 && typeOfData != 2)
        {
            System.out.println("Error: Invalid type.");
            System.exit(1);
        }

        // reading the data
        EnglishGrade[] arr = new EnglishGrade[arraySize];
        for(int i = 0; i < arr.length; ++i)
        {
            System.out.print("Enter first " + (typeOfData == 1 ? "English" : "Math") + " test grade: ");
            int firstGrade = Integer.parseInt(br.readLine());
            System.out.print("Enter second " + (typeOfData == 1 ? "English" : "Math") + " test grade: ");
            int secondGrade = Integer.parseInt(br.readLine());
            arr[i] = typeOfData == 1? new EnglishGrade(firstGrade, secondGrade) : new MathGrade(firstGrade, secondGrade);
        }

        // displaying the data
        System.out.println("Sorting:");

        bw.write("Sorting:");
        bw.newLine();

        for(int i = 0; i < arr.length; ++i)
        {
            System.out.println("First " + (typeOfData == 1 ? "English" : "Math") + " grade: " + arr[i].getfirstEnglishGrade() + "\tsecond " + (typeOfData == 1 ? "English" : "Math") + " grade: " + arr[i].getsecondEnglishGrade());

            bw.write("First " + (typeOfData == 1 ? "English" : "Math") + " grade: " + arr[i].getfirstEnglishGrade() + "\tsecond " + (typeOfData == 1 ? "English" : "Math") + " grade: " + arr[i].getsecondEnglishGrade());
            bw.newLine();
        }
        System.out.println();
        bw.newLine();

        // calling merge sort
        recursionMergeSort(arr, 0, arr.length - 1);

        // closing buffers
        br.close();
        bw.close();
    }

    // performs a recursive merge sort on arr[l, l+1, ..., r]
    public static void recursionMergeSort(EnglishGrade[] arr, int l, int r) throws IOException
    {
        if(l < r)
        {
            int mid = l + (r - l) / 2;
            recursionMergeSort(arr, l, mid);
            recursionMergeSort(arr, mid + 1, r);
            merge(arr, l, mid, r);

            for(int i = 0; i < arr.length; ++i)
            {
                System.out.println("First " + (typeOfData == 1 ? "English" : "Math") + " grade: " + arr[i].getfirstEnglishGrade() + "\tsecond " + (typeOfData == 1 ? "English" : "Math") + " grade: " + arr[i].getsecondEnglishGrade());
                bw.write("First " + (typeOfData == 1 ? "English" : "Math") + " grade: " + arr[i].getfirstEnglishGrade() + "\tsecond " + (typeOfData == 1 ? "English" : "Math") + " grade: " + arr[i].getsecondEnglishGrade());
                bw.newLine();
            }
            System.out.println();
            bw.newLine();
        }
    }

    // merges the sorted subarrays arr[l, l+1, ..., mid] and arr[mid+1, mid+2, ..., r]
    public static void merge(EnglishGrade[] arr, int l, int mid, int r)
    {
        // creating the auxilliary array
        EnglishGrade[] temp = new EnglishGrade[r - l + 1];
        for(int i = 0; i < r - l + 1; ++i)
        {
            temp[i] = new EnglishGrade();
        }

        // merging
        int k = 0;
        int i = l, j = mid + 1;
        while(i <= mid && j <= r)
        {
            if(arr[i].getfirstEnglishGrade() + arr[i].getsecondEnglishGrade()
                < arr[j].getfirstEnglishGrade() + arr[j].getsecondEnglishGrade())
            {
                temp[k].setfirstEnglishGrade(arr[i].getfirstEnglishGrade());
                temp[k].setsecondEnglishGrade(arr[i].getsecondEnglishGrade());
                ++i;
            }
            else
            {
                temp[k].setfirstEnglishGrade(arr[j].getfirstEnglishGrade());
                temp[k].setsecondEnglishGrade(arr[j].getsecondEnglishGrade());
                ++j;
            }
            ++k;
        }

        while(i <= mid)
        {
            temp[k].setfirstEnglishGrade(arr[i].getfirstEnglishGrade());
            temp[k].setsecondEnglishGrade(arr[i].getsecondEnglishGrade());
            ++i;
            ++k;
        }

        while(j <= r)
        {
            temp[k].setfirstEnglishGrade(arr[j].getfirstEnglishGrade());
            temp[k].setsecondEnglishGrade(arr[j].getsecondEnglishGrade());
            ++j;
            ++k;
        }

        // copying the data from the auxilliary array to the actual array
        for(k = 0; k < r - l + 1; ++k)
        {
            arr[k + l].setfirstEnglishGrade(temp[k].getfirstEnglishGrade());
            arr[k + l].setsecondEnglishGrade(temp[k].getsecondEnglishGrade());
        }
    }

    // member variables
    private static BufferedReader br;
    private static BufferedWriter bw;
    private static int typeOfData;
}

Output:

❯ javac EnglishGrade.java Main.java MathGrade.java
❯ java Main
Enter the number of elements: 5
Types of data: 
1. EnglishGrade
2. MathGrade
Enter the type of data you want to enter: 1
Enter first English test grade: 10
Enter second English test grade: 9
Enter first English test grade: 8
Enter second English test grade: 7
Enter first English test grade: 6
Enter second English test grade: 5
Enter first English test grade: 4
Enter second English test grade: 3
Enter first English test grade: 2
Enter second English test grade: 1
Sorting:
First English grade: 10 second English grade: 9
First English grade: 8  second English grade: 7
First English grade: 6  second English grade: 5
First English grade: 4  second English grade: 3
First English grade: 2  second English grade: 1

First English grade: 8  second English grade: 7
First English grade: 10 second English grade: 9
First English grade: 6  second English grade: 5
First English grade: 4  second English grade: 3
First English grade: 2  second English grade: 1

First English grade: 6  second English grade: 5
First English grade: 8  second English grade: 7
First English grade: 10 second English grade: 9
First English grade: 4  second English grade: 3
First English grade: 2  second English grade: 1

First English grade: 6  second English grade: 5
First English grade: 8  second English grade: 7
First English grade: 10 second English grade: 9
First English grade: 2  second English grade: 1
First English grade: 4  second English grade: 3

First English grade: 2  second English grade: 1
First English grade: 4  second English grade: 3
First English grade: 6  second English grade: 5
First English grade: 8  second English grade: 7
First English grade: 10 second English grade: 9

❯ cat output.txt
Sorting:
First English grade: 10 second English grade: 9
First English grade: 8  second English grade: 7
First English grade: 6  second English grade: 5
First English grade: 4  second English grade: 3
First English grade: 2  second English grade: 1

First English grade: 8  second English grade: 7
First English grade: 10 second English grade: 9
First English grade: 6  second English grade: 5
First English grade: 4  second English grade: 3
First English grade: 2  second English grade: 1

First English grade: 6  second English grade: 5
First English grade: 8  second English grade: 7
First English grade: 10 second English grade: 9
First English grade: 4  second English grade: 3
First English grade: 2  second English grade: 1

First English grade: 6  second English grade: 5
First English grade: 8  second English grade: 7
First English grade: 10 second English grade: 9
First English grade: 2  second English grade: 1
First English grade: 4  second English grade: 3

First English grade: 2  second English grade: 1
First English grade: 4  second English grade: 3
First English grade: 6  second English grade: 5
First English grade: 8  second English grade: 7
First English grade: 10 second English grade: 9

Hope this helps! :)


Related Solutions

.......Subject Java..... main() main() will ask the user for input and then call functions to do...
.......Subject Java..... main() main() will ask the user for input and then call functions to do calculations. The calculations will be returned to main() where they will be printed out. First function Create a function named computeBill that receives on parameter. It receives the price of an item. It will then add 8.25% sales tax to this and return the total due back to main(). Second function Create another function named computeBill that receives 2 parameters. It will receive the...
USING JAVA (netbeans) In your main, ask the user for an int. Do this by first...
USING JAVA (netbeans) In your main, ask the user for an int. Do this by first printing a request for the int, then creating an int x, and using the Scanner to read an int from the user and put it in x, like this: int x = scan.nextInt(); ☑ Next read in two doubles d1 and d2 from the user. This will look a lot like what you did for the int, but the scanner reads doubles using scan.nextDouble();...
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
Q3) ​Write a main function to test your SortTriple will ask the user to enter the...
Q3) ​Write a main function to test your SortTriple will ask the user to enter the three values, then uses ​SortTriple ​to reorder the values if required. The main function should print the value in the correct order and a message to indicate if the values were in the correct order or not. ​[20 points] ● You are NOT allowed to use loops or arrays in your code​ ​[- 30 points] ● You are ​NOT​ allowed to use global variables....
i need a conclusion for my paper on ADHD. I need this asap please have to...
i need a conclusion for my paper on ADHD. I need this asap please have to turn my paper in one hour!!!! thank you            In the recent times, ADHD is being seen in the light of cerebral dysfunction with too much focus on clinical treatment using medication at the same time keeping psychotherapy at bay which shows a dangerous trend. Psychotherapy even though cannot explain the cause of the condition, but it can help the children in coping his...
I need this in Java please: Lab11B: Understanding the constructor. Remember that the constructor is just...
I need this in Java please: Lab11B: Understanding the constructor. Remember that the constructor is just a special method (with no return type) that has the same name as the class name. Its job is to initialize all of the attributes. You can actually have more than one constructor, so long as the parameters are different. Create a class called Turtle that has two attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user to enter a short sentence which ends in a period. Use Java String class methods to determine the following about the sentence and display in the console: • How many total characters are in the sentence? • What is the first word of the sentence? Assume the words are separated by a space. • How many characters are in the first word of the...
I need to draw a cylinder in java with user input, and I can't seem to...
I need to draw a cylinder in java with user input, and I can't seem to get my lines to line up with my ovals correctly from the users input... I know I will have to either add or subtract part of the radius or height but I'm just not getting it right, here is how I'm looking to do it.            g.drawOval(80, 110, radius, height);            g.drawLine(?, ?, ?, ?); g.drawLine(?, ?, ?, ?);   ...
I need to ask a user what numbers they want to enter. They can enter as...
I need to ask a user what numbers they want to enter. They can enter as many as they like. Then inside I need to use conditionals to determine if the numbers are <=20, <=323 && > 30, >200. I can't figure out how to let the user enter as many inputs as they want. I know I need to use a loop to check each number entered and determine if it is small middle or greater but I can't...
please, I need it with 30 minutes . can someone solve this ASAP and post ,...
please, I need it with 30 minutes . can someone solve this ASAP and post , I really appreciate. 4. "Machine A has an immediate cost of $15,000, and it will earn a net income of $4300 per year for a total of 7 years. Machine B has an immediate cost of $17,000, and it will earn a net income of $4400 per year for a total of 35 years. Assume that Machine A can continually be replaced at the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT