The language is java. Please create a simple code using the prompt with all four
ID: 3714531 • Letter: T
Question
The language is java. Please create a simple code using the prompt with all four questions. It shouldn’t take long and this a study guide question to practice for the test. Thank you.
the provided classes needed are listed down below:
public class Course
{
private String title;
private int courseNumber;
private int grades[];
//initial values constructor
public Course(String t, int cn, int g[]){
title=t;
courseNumber=cn;
grades=g;
}
//get methods
public String getTitle(){return title;}
public int getCourseNum(){return courseNumber;}
public int[] getGrades(){return grades;}
//method to find average grade
public double Average(){
double sum=0;
for(int i=0; i sum=sum+grades[i];
return sum/grades.length; // return average
}
//method that return maxim grade for a class
public int maxGrade(){
int max=grades[0];
for(int i=1;i if(grades[i]>max)
max=grades[i];
return max;
}
public String toString(){
return String.format("%3d %s",courseNumber,title);
}
}
---------------------------------------
public class Student
{
private String name;
private Course listOfCourse[];
//initial valued constructor
public Student(String n, Course list[]){
name=n;
listOfCourse=list;
}
//get methods
public Course[] getList(){return listOfCourse;}
public String getName(){return name;}
//method to determine best courses based on Average grade
//method returns the course with best average grade
public Course Best(){
Course c;
c=listOfCourse[0];
for(int i=1;i if(c.Average() c=listOfCourse[i];
return c;
}
//toString method
public String toString(){
return name;
}
}
---------------------------------------
public class Instructor
{
private String name;
private Student list[]; //list of students
//inial valued constructor
public Instructor(String n, Student s[])
{
name=n;
list=s;
}
//get methods
public Student[] getStudent()
{
return list;
} // returns all the students of an instructor
}
---------------------------------------
public class Test {
public static void main(String[] args)
{
int g1[]={90,89,40,70};
int g2[]={100,90,80,70,100};
int g3[]={89,95,80,70};
int g4[]={99,79,80,60};
int g5[]={100,100,80,90,100};
int g6[]={100,50,80,95,100};
Course list1[]=new Course[4];
list1[0]=new Course("Programming 1", 180,g1);
list1[1]=new Course("History", 120, g2);
list1[2]=new Course("Mathematics", 136,g3);
list1[3]=new Course("Data Mining", 251,g4);
Course list2[]=new Course[2];
list2[0]=new Course("Art", 104,g5);
list2[1]=new Course("Music", 100,g6);
Student L[]=new Student[2];
L[0]=new Student("Sara", list1);
L[1]=new Student("Mike",list2);
Instructor A=new Instructor("James Gary",L);
}
}
Explanation / Answer
Below is your code. Your Question 4 was not that clear but still I have implemented it. Please let me know if you have any issues. I'll help for sure. Dont forget to rate positive If I was able to help
Course.java
public class Course {
private String title;
private int courseNumber;
private int grades[];
// initial values constructor
public Course(String t, int cn, int g[]) {
title = t;
courseNumber = cn;
grades = g;
}
// get methods
public String getTitle() {
return title;
}
public int getCourseNum() {
return courseNumber;
}
public int[] getGrades() {
return grades;
}
// method to find average grade
public double Average() {
double sum = 0;
for (int i = 0; i < grades.length; i++)
sum = sum + grades[i];
return sum / grades.length; // return average
}
// method that return maxim grade for a class
public int maxGrade() {
int max = grades[0];
for (int i = 0; i < grades.length; i++) {
if (grades[i] > max)
max = grades[i];
}
return max;
}
public String toString() {
String str = "";
str = str + "Course Name: "+getTitle()+" ";
str = str + "Course Numbers: "+getCourseNum()+" ";
str = str + "Grades: ";
for (int i = 0; i < grades.length; i++) {
str = str + grades[i] + " ";
}
return str;
}
}
Student.java
public class Student {
private String name;
private Course listOfCourse[];
// initial valued constructor
public Student(String n, Course list[]) {
name = n;
listOfCourse = list;
}
// get methods
public Course[] getList() {
return listOfCourse;
}
public String getName() {
return name;
}
// method to determine best courses based on Average grade
// method returns the course with best average grade
public Course Best() {
Course c;
c = listOfCourse[0];
for (int i = 1; i < listOfCourse.length; i++) {
if (c.Average() < listOfCourse[i].Average())
c = listOfCourse[i];
}
return c;
}
// toString method
public String toString() {
return name;
}
public int[] max() {
int[] maxGrades = new int[listOfCourse.length];
for (int i = 0; i < listOfCourse.length; i++) {
maxGrades[i] = listOfCourse[i].maxGrade();
}
return maxGrades;
}
}
Instructor.java
public class Instructor {
private String name;
private Student list[]; // list of students
// inial valued constructor
public Instructor(String n, Student s[]) {
name = n;
list = s;
}
// get methods
public Student[] getStudent() {
return list;
} // returns all the students of an instructor
//insertion sort of student list by name
public void sort() {
int n = list.length;
for (int i = 1; i < n; ++i) {
Student key = list[i];
int j = i - 1;
while (j >= 0 && list[j].getName().compareTo(key.getName()) > 0) {
list[j + 1] = list[j];
j = j - 1;
}
list[j + 1] = key;
}
}
}
Test.java
public class Test {
public static void main(String[] args) throws FileNotFoundException {
int g1[] = { 90, 89, 40, 70 };
int g2[] = { 100, 90, 80, 70, 100 };
int g3[] = { 89, 95, 80, 70 };
int g4[] = { 99, 79, 80, 60 };
int g5[] = { 100, 100, 80, 90, 100 };
int g6[] = { 100, 50, 80, 95, 100 };
Course list1[] = new Course[4];
list1[0] = new Course("Programming 1", 180, g1);
list1[1] = new Course("History", 120, g2);
list1[2] = new Course("Mathematics", 136, g3);
list1[3] = new Course("Data Mining", 251, g4);
Course list2[] = new Course[2];
list2[0] = new Course("Art", 104, g5);
list2[1] = new Course("Music", 100, g6);
Student L[] = new Student[2];
L[0] = new Student("Sara", list1);
L[1] = new Student("Mike", list2);
Instructor A = new Instructor("James Gary", L);
getInput();
}
public static void getInput() throws FileNotFoundException {
Scanner scan = new Scanner(new File("input.txt"));
int numOfCourses = Integer.parseInt(scan.nextLine());
int[] grades = new int[numOfCourses];
int sum = 0;
for (int i = 0; i < grades.length; i++) {
grades[i] = scan.nextInt();
sum = sum + grades[i];
}
scan.nextLine();
String idNum = scan.nextLine();
String name = scan.nextLine();
System.out.println("Student ID number = "+idNum);
System.out.println("Student Name: "+name);
System.out.println("Average of grades = "+sum/(double)grades.length);
}
}
input.txt
6
10 20 30 40 50 60
800-00-1111
Bobby J Icita
Output
Student ID number = 800-00-1111
Student Name: Bobby J Icita
Average of grades = 35.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.