Write a class encapsulating the concept of student grades on a test, assuming st
ID: 3775923 • Letter: W
Question
Write a class encapsulating the concept of student grades on a test, assuming student grades are composed of a list of integers between 0 and 100. Write the following methods: A constructor with just one parameter, the number of students; all grades can be randomly generated Accessor, mutator, toString, and equals methods A method returning an array of the grades sorted in ascending order A method returning the highest grade A method returning the average grade A method returning the median grade (Hint: The median grade will be located in the middle of the sorted array of grades.) A method returning the mode (the grade that occurs most often) (Hint: Create an array of counters; count how many times each grade occurs; then pick the maximum in the array of counters; the array index is the mode.) Write a client class to test all the methods in your class.
This Java program MUST include two separate classes. One encapsulated class is to be named Grades. Here is a sample output:
Please use the following java program as a guide:
import java.util.Scanner;
public class Temperature {
/**
* @param args
*/
public static void main(String[] args) {
int[] temperatures1 = new int[7];
int[] temperatures2;
Scanner keyboard = new Scanner(System.in);
System.out.println(" Enter this week's termperatures ");
for(int i = 0; i < temperatures1.length; i++)
{
temperatures1[i] = keyboard.nextInt();
}
Temperature temps1 = new Temperature(temperatures1);
//call toString
System.out.println(" temps1: " +temps1+ " ");
//Find the highest temperature between any two consecutive days and display it
System.out.println(" The highest temperature for temp1 between any two consecutive days is: " +temps1.largestChange() + " degrees ");
//Display how many of the entered temperatures are below freezing
System.out.println(" Temperatures entered below 33 degrees for temps1: " +temps1.belowFreezing() + " ");
if(temps1.getCountAbove() > 0)
{
int[] above100 = temps1.temperaturesAbove100();
//Display temperatures above 100 and display
System.out.println("Temperatures above 100 for temps1 are: ");
for(int i = 0; i < above100.length; i++)
{
System.out.println(above100[i] + " ");
System.out.println();
}
}
//sort array and display again
temps1.sortArray();
System.out.println(" Sorted temperatures for temps1 are " +temps1+ " ");
//set the second array to equal what is returned by the getTestTemperatures() method
//create a new Temperature object based on the test temperatures
Temperature temps2 = new Temperature();
temperatures2 = temps2.getTestTemperatures();
//call toString
System.out.println(" temps2: " +temps2+ " ");
//Find the highest temperature between any two consecutive days and display it
System.out.println(" The highest temperature for temps2 between any two consecutive days is: " +temps2.largestChange() + " degrees ");
//Display how many of the entered temperatures are below freezing
System.out.println(" Temperatures entered below 33 degrees for temps2: " +temps2.belowFreezing() + " ");
if(temps2.getCountAbove() > 0)
{
int[] above100Temps2 = temps2.temperaturesAbove100();
//Display temperatures above 100 display
System.out.println("Temperatures above 100 for temps1 are: ");
for(int i = 0; i < above100Temps2.length; i++)
{
System.out.println(above100Temps2[i] + " ");
System.out.println();
}
}
else
System.out.println("There are no temperatures above 100");
//sort array and display again
temps2.sortArray();
System.out.println(" Sorted temperatures for temps2 are " +temps2+ " ");
//test equals
System.out.println(" Testing to see if the of two objects are equal");
if(temps1.equals(temps2))
System.out.println(" Objects are not equal ");
//create identical objects by setting the two objects to equal each other
System.out.println(" Setting temps1 and temps2 arrays to entered temperatures ");
temps2 = temps1;
//displaying new values for both objects
System.out.println("temps1: " +temps1);
System.out.println("temps2: " +temps2);
System.out.println(" Testing to see if the two objects are equals ");
if(temps1.equals(temps2))
System.out.println(" Objects are equal ");
else
System.out.println(" Objects are not equal ");
}
}
Problems Javadoc Declaration Console XExplanation / Answer
import java.util.Random;
public class Convertgrade1
{
private int nos;
private int[] sg = new int[100];
public Convertgrade1()
{
nos=0;
}
public Convertgrade1(int students)
{
nos = students;
setgrades(students);
}
public void setstudents(int students)
{
if(students<0)
{
nos=0;
}
else
{
nos=students;
setgrades(students);
}
}
public int getstudents()
{
return nos;
}
public void setgrades(int students)
{
Random rn = new Random();
for(int c=0;c<students;c++)
{
sg[c] = rn.nextInt(101);
}
}
public char[] getlettergrades()
{
char[] lg = new char[nos];
for(int c=0;c<nos;c++)
{
if(sg[c]<=59)
lg[c] = 'F';
else if(sg[c]<=69)
lg[c] = 'D';
else if(sg[c]<=79)
lg[c] = 'C';
else if(sg[c]<=89)
lg[c] = 'B';
else if(sg[c]<=100)
lg[c] = 'A';
}
return lg;
}
public int getas()
{
int n1=0;
for(int c=0;c<nos;c++)
{
if(sg[c]>=90)
n1++;
}
return n1;
}
public int[] getnumberofeachgrade()
{
int[] nog =new int[nos];
for(int c=0;c<nos;c++)
{
if(sg[c]<=59)
nog[c]++;
else if(sg[c]<=69)
nog[c]++;
else if(sg[c]<=79)
nog[c]++;
else if(sg[c]<=89)
nog[c]++;
else if(sg[c]<=100)
nog[c]++;
}
return nog;
}
public String tostring()
{
System.out.format("The number of students = %d ",nos);
for(int c=0;c<nos;c++)
{
System.out.format("Grade % d is %d ",c+1,sg[c]);
}
return String.format(" ");
}
public void sortarray(int[] array)
{
int t,c=0,i=0;
for(c=0;c<(array.length-1);c++)
{
for(i=0;i<(array.length-1);i++)
{
if(array[i] > array[i+1])
{
t=array[i+1];
array[i+1]=array[i];
array[i]=t;
}
}
}
}
}
//client class
public class Convertgradetestclient1
{
public static void main(String[] args)
{
Convertgrade1 m1 = new Convertgrade1();
Convertgrade1 m2 = new Convertgrade1(5);
char[] grades = new char[100];
m1.tostring();
m2.tostring();
m1.setstudents(8);
m1.tostring();
grades = m1.getlettergrades();
System.out.println(grades);
grades = m2.getlettergrades();
System.out.println(grades);
System.out.format("The number of A's = %d ",m1.getas());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.