In: Computer Science
IN JAVA PLEASE ASAP !!!
I just need the main and mergesort function
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;
}
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! :)