Question: Java Help for COMP. There are three parts to this problem. Please help. 1. (a). Suppose we have the following: int a=37; int b=5; //you
Java Help for COMP. There are three parts to this problem. Please help.
1. (a). Suppose we have the following: int a=37; int b=5;
//you can also initialize this as // int a=37,b=5; System.out.println(a - a/b*b -a%b);
Guess ahead what the output will be. Write a small program to test if your guess/prediction is true. Also, do you expect the same output to come out in any other programming language?
_________________________________________________________________________________________________________
(b). In the attached zip file, you will find two files HighestGPA.java and students.dat. Students.dat file contains names of students and their gpa. Check if the code runs correctly in BlueJ or any other IDE by running the code. Modify the data to include few more students and their gpa.
Then modify the code to create LowestGPA.java that prints out the name of the student with the lowest gpa.
import java.util.*; // for the Scanner class
import java.io.*; // for the File class
public class HighestGPA { public static void main (String[ ] args) throws FileNotFoundException { new HighestGPA().run(); } // method main
public void run() throws FileNotFoundException { final double NEGATIVE_GPA = -1.0;
final String NO_VALID_INPUT = "Error: the given file has no valid input.";
final String BEST_MESSAGE = " The student with the highest grade point average is ";
Scanner fileScanner = new Scanner (new File ("students.dat"));
String name, bestStudent = null;
double gpa, highestGPA = NEGATIVE_GPA;
while (fileScanner.hasNextLine()) { Scanner lineScanner = new Scanner (fileScanner.nextLine());
name = lineScanner.next(); gpa = lineScanner.nextDouble(); if (gpa > highestGPA) { highestGPA = gpa; bestStudent = name; } // if } // while if (highestGPA == NEGATIVE_GPA) System.out.println (NO_VALID_INPUT); else System.out.println (BEST_MESSAGE + bestStudent); } // method run
} // class HighestGPA
Students.dat file:
Larry 3.3 Curly 3.7 Moe 3.2
_________________________________________________________________________________________________________
(c). Nothing.java program is supposed to print 3 times the value of something. Check if it does. If it does not work, (i) write why it does not work correctly, and (ii) fix the code to print the correct value.
public class Nothing { public static void main (String[ ] args) { new Nothing().run(); } // method main
public void run() { int k = 30; triple (k); System.out.println (k); } // method run
public void triple (int n) { n = n * 3; } // method triple
} // class Nothing
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
