Question

In: Computer Science

public static Object[] question4(Student student1, Student student2) { /* For this exercise you will be using...

public static Object[] question4(Student student1, Student student2)
{
/* For this exercise you will be using for loops to calculate various values.
You will be making use of the following object references which are passed as arguments to this method:
A Student object reference student1
A Student object reference student2
You will need to use various accessor methods of the Student class to complete this assignment.
Additional variables that you will use have already been declared.

1) Set the value of student1HighestGrade to the highest grade for student1
2) Set the value of student2HighestGrade to the highest grade for student2
3) Set the value of student1AverageGrade to the average grade for student1
4) Set the value of student2AverageGrade to the average grade for student2
5) Assign the bestHighGradeStudent object reference whichever student has the best high grade
6) Assign the bestAverageGradeStudent object reference whichever student has the best average grade

This program contains a main method that can be used to manually test your code by right-clicking Question4.java
and selecting "Run File"   
*/
  
int student1HighestGrade, student2HighestGrade;
double student1AverageGrade, student2AverageGrade;
Student bestAverageGradeStudent, bestHighGradeStudent;
  
// Your code goes Here:
  
  
  
  
  
  
  
  

  
// Necessary for Unit Test
return new Object[] {student1HighestGrade, student2HighestGrade, student1AverageGrade, student2AverageGrade, bestHighGradeStudent, bestAverageGradeStudent};
}
  
public static void main(String[] args)
{
Student s1 = new Student();
Student s2 = new Student();
s2.setName("John Doe");
  
System.out.println("Student1: " + s1);
System.out.println("Student2: " + s2);
  
Object[] o = question4(s1, s2);
  
System.out.println("student1HighestGrade = " + (int)o[0]);
System.out.println("student2HighestGrade = " + (int)o[1]);
System.out.println("student1AverageGrade = " + (double)o[2]);
System.out.println("student2AverageGrade = " + (double)o[3]);
System.out.println("bestHighGradeStudent = " + ((Student)o[4]).getName());
System.out.println("bestAverageGradeStudent = " + ((Student)o[5]).getName());
}
}

here is student class

package ProvidedClasses;

import java.util.Objects;
import java.util.Random;


public class Student
{
private String studentName;
private int studentID;
private int[] examScores = new int[8];
  
/**
* Construct a Student object with the following default values:
* studentName = "John Doe"
* studentID = 1111111;
*   
*/
public Student()
{
studentName = "Jane Doe";
studentID = 0012345;
  
Random rand = new Random();
  
for (int i = 0; i < 8; i++)
{
examScores[i] = rand.nextInt(26) + 75;
}
}
  
/**
*
* @param name A String representing the student's name.
* @param ID An int representing the student's ID number.
*/
public Student(String name, int ID)
{

setName(name);
setID(ID);
for (int i = 1; i <= 8; i++)
{
this.setExamScore(i-1, 0);
}
  
}
  
public static Student getAStudentInstance()
{
Student s = new Student("Suzy Q", 1010156);
for (int i = 1; i <=8; i++)
{
s.setExamScore(i-1, 90 + i);
}
  
return s;
}
  
public static Student getBStudentInstance()
{
Student s = new Student("Joey D", 2349817);
for (int i = 1; i <=7; i++)
{
s.setExamScore(i-1, 76 + (i * 2));
}
  
s.setExamScore(7, 100);
  
return s;
}
  
/**
*
* @param newName A String representing the new value to set the student's name to.
*/
public void setName(String newName)
{
studentName = newName;
}
  
/**
*
* @param newID An int representing the new value to set the student's ID to.
*/
private void setID(int newID)
{
studentID = Math.abs(newID % 10000000);
}
  
  
/**
*
* @param index The index of the exam to get (Should be 0 - 7 inclusive).
* @param score The score for the exam.
*/
public void setExamScore(int index, int score)
{
examScores[index] = score;
}
  
/**
*
* @return The name of the student.
*/
public String getName()
{
return studentName;
}
  
/**
*
* @return The ID number of the student.
*/
public int getID()
{
return studentID;
}
  
/**
*
* @param index The index to get the score for. Should only be 0 - 7 inclusive.
* @return The score for the specified exam.
*/
public int getExamScore(int index)
{
return examScores[index];
}
  

  
/**
*
* @param o An Object to check against for equality
* @return Whether or not the Student object is equal to the o
*/
@Override
public boolean equals(Object o)
{
if (o instanceof Student)
{
Student s = (Student)o;
return (studentName.equals(s.getName()) && studentID == s.getID());
}
return false;
}

/**
*
* @return An int representing the hash of the object.
*/
@Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + Objects.hashCode(this.studentName);
hash = 59 * hash + this.studentID;
return hash;
}


/**
*
* @return A String representing the object.
*/
@Override
public String toString()
{
String toReturn = "(" + studentName + ", " + studentID + ", [";
for (int grade : examScores)
{
toReturn += grade + ", ";
}
toReturn += "])";
  
return toReturn;
}
}

Solutions

Expert Solution

Completed function code:

   public static Object[] question4(Student student1, Student student2){
   /* For this exercise you will be using for loops to calculate various values.
   You will be making use of the following object references which are passed as arguments to this method:
   A Student object reference student1
   A Student object reference student2
   You will need to use various accessor methods of the Student class to complete this assignment.
   Additional variables that you will use have already been declared.

   1) Set the value of student1HighestGrade to the highest grade for student1
   2) Set the value of student2HighestGrade to the highest grade for student2
   3) Set the value of student1AverageGrade to the average grade for student1
   4) Set the value of student2AverageGrade to the average grade for student2
   5) Assign the bestHighGradeStudent object reference whichever student has the best high grade
   6) Assign the bestAverageGradeStudent object reference whichever student has the best average grade

   This program contains a main method that can be used to manually test your code by right-clicking Question4.java
   and selecting "Run File"   
   */
  
   int student1HighestGrade, student2HighestGrade;
   double student1AverageGrade, student2AverageGrade;
   Student bestAverageGradeStudent, bestHighGradeStudent;
  
   // Your code goes Here:
  
  
  
   //FINDING HIGHEST GRADE OF STUDENT 1
   student1HighestGrade = -1;   //initially set to minimum
   //iterating through all 8 grades
   for(int i = 0; i < 8; i++){
       //current highest grade is less than ith grade
       if(student1.getExamScore(i) > student1HighestGrade){
           //making ith grade as the highest grade
           student1HighestGrade = student1.getExamScore(i);
       }
   }
  
   //FINDING HIGHEST GRADE OF STUDENT 1
   student2HighestGrade = -1;   //initially set to minimum
   //iterating through all 8 grades
   for(int i = 0; i < 8; i++){
       //current highest grade is less than ith grade
       if(student2.getExamScore(i) > student2HighestGrade){
           //making ith grade as the highest grade
           student2HighestGrade = student2.getExamScore(i);
       }
   }
  
   //FINDING THE GRADE SUM OF STUDENT 1
   student1AverageGrade = 0;   //setting grade sum as 0 initially
   //iterating through all 8 grades
   for(int i = 0; i < 8; i++){
       //adding ith grade to tha total grade sum
       student1AverageGrade += student1.getExamScore(i);
   }
   //FINDING THE AVERAGE GRADE OF STUDENT 1
   student1AverageGrade /= 8;
  
  
   //FINDING THE GRADE SUM OF STUDENT 2
   student2AverageGrade = 0;   //setting grade sum as 0 initially
   //iterating through all 8 grades
   for(int i = 0; i < 8; i++){
       //adding ith grade to tha total grade sum
       student2AverageGrade += student2.getExamScore(i);
   }
   //FINDING THE AVERAGE GRADE OF STUDENT 2
   student2AverageGrade /= 8;
  
  
   //FINDING THE BEST HIGHEST GRADE STUDENT
   if(student1HighestGrade > student2HighestGrade){
       //student1's highest grade is greater than student2's highest grade
       //Best highest grade student is student1
       bestHighGradeStudent = student1;
   }
   else{
       //Best highest grade student is student2
       bestHighGradeStudent = student2;
   }
  
  
   //FINDING THE BEST AVERAGE GRADE STUDENT
   if(student1AverageGrade > student2AverageGrade){
       //student1's average grade is greater than student2's highest grade
       //Best average grade student is student1
       bestAverageGradeStudent = student1;
   }
   else{
       //Best average grade student is student2
       bestAverageGradeStudent = student2;
   }
  
  
  
   // Necessary for Unit Test
   return new Object[] {student1HighestGrade, student2HighestGrade, student1AverageGrade, student2AverageGrade, bestHighGradeStudent, bestAverageGradeStudent};
   }

Full Code Screenshot (highlighted the important parts of the code):

Output:


Related Solutions

The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
import java.util.LinkedList; public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new...
import java.util.LinkedList; public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new LinkedList<Student>(); linkedlist.add(new Student("Ahmed Ali", 20111021, 18, 38, 38)); linkedlist.add(new Student("Sami Kamal", 20121021, 17, 39, 35)); linkedlist.add(new Student("Salem Salim", 20131021, 20, 40, 40)); linkedlist.add(new Student("Rami Mohammed", 20111031, 15, 35, 30)); linkedlist.add(new Student("Kim Joe", 20121024, 12, 32, 32)); linkedlist.addFirst(new Student("Hadi Ali", 20111025, 19, 38, 39)); linkedlist.addLast(new Student("Waleed Salim", 20131025, 10, 30, 30)); linkedlist.set(0, new Student("Khalid Ali", 20111027, 15, 30, 30)); linkedlist.removeFirst(); linkedlist.removeLast(); linkedlist.add(0, new Student("John Don",...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void main(String[] args) { Rectangle r1 = new Rectangle(0,0,100,150); System.out.println(r1);    Rectangle r2 = new Rectangle(50,75,100,150); System.out.println(r2);    Rectangle r3 = r1.intersection(r2);    } } Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the...
public static void main(String[] args) {            //Part1: using three stacks           ...
public static void main(String[] args) {            //Part1: using three stacks            iQueue<Integer> main = new ourLinkedList<Integer>();                       for (int i = 0; i<10; i++) {                int num = -15 + (int) (Math.random() * ((15 - (-15)) + 1));            main.add(num);            }                       System.out.println("main: " +main);            iQueue<Integer> Q1 = new ourLinkedList<Integer>();           ...
Could you do it with python please? public class BSTTraversal {         public static void main(String[]...
Could you do it with python please? public class BSTTraversal {         public static void main(String[] args) {                 /**                  * Creating a BST and adding nodes as its children                  */                 BSTTraversal binarySearchTree = new BSTTraversal();                 Node node = new Node(53);                 binarySearchTree.addChild(node, node, 65);                 binarySearchTree.addChild(node, node, 30);                 binarySearchTree.addChild(node, node, 82);                 binarySearchTree.addChild(node, node, 70);                 binarySearchTree.addChild(node, node, 21);                 binarySearchTree.addChild(node, node, 25);                 binarySearchTree.addChild(node, node, 15);                 binarySearchTree.addChild(node, node, 94);                 /**         ...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static)...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static) method named firstOne. firstOne accepts a String array and returns a map from Strings to Integer. The map must count the number of passed Strings based on the FIRST letter. For example, with the String array {“banana”, “apples”, “blueberry”, “orange”}, the map should return {“b”:2, “a”:1, “o”:1}. Disregard empty Strings and zero counts. Retrieve first character of String as a char using charAt, or,...
Required methods:: public static void processAccount (Account[] acc, printWriter out{}. public static boolean deposit(Account a){}.
Java programming. Required methods::public static void processAccount (Account[] acc, printWriter out{}.public static boolean deposit(Account a){}.public static boolean withdrawal(Account a){}.public static int findAccount(long accountNumber, Account[] acc){}. 1/ Remove the applyRandomBonus method from the test class2/ Create a File, Printwriter for an output file yourlastnameErrorLog.txt4/ Catch InputMismatch and ArrayIndexOutOfBounds exceptions when reading data from the file:a. Skip any lines that cause an exceptionb. Write information about the exception to the log file, yourlastnameError.txtc. Include exception type and line number in exception message...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT