Question

In: Computer Science

JAVA: How do I fix the last "if" statement in my code so that outputs the...

JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes?



public class app {

private static Object minStudent;
private static Object maxStudent;


public static void main(String args[ ])
{
student st1 = new student("Rebecca", "Collins", 22, 3.3);
student st2 = new student("Alex", "White", 19, 2.8);
student st3 = new student("Jordan", "Anderson", 22, 3.1);

student[] studentArray;
studentArray = new student[3];

studentArray[0] = st1;
studentArray[1] = st2;
studentArray[2] = st3;


for (student studentArray1 : studentArray) {
  
  
int min; min = (int) studentArray[0].gpa;
int max; max = (int) studentArray[0].gpa;
System.out.println("Name: " + studentArray1.firstName + " " + studentArray1.lastName);
System.out.println("Age: " + studentArray1.age);
System.out.println("GPA: " + studentArray1.gpa);
{
  
if (studentArray1.gpa < max) {
System.out.println(studentArray1.firstName + " has the minimun GPA of " + studentArray1.gpa);
}
if (studentArray1.gpa >= max) {
System.out.println(studentArray1.firstName + " has the maximun GPA of " + studentArray1.gpa);
}
if(studentArray1.gpa > min){
System.out.println(studentArray1.firstName + " has the second maximum GPA of " + studentArray1.gpa);
}
  
}
}
  
}

}

Solutions

Expert Solution

class student {
    public String firstName;
    public String lastName;
    public int age;
    public double gpa;

    public student(String firstName, String lastName, int age, double gpa) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.gpa = gpa;
    }
}

class app {

    private static Object minStudent;
    private static Object maxStudent;


    public static void main(String args[]) {
        student st1 = new student("Rebecca", "Collins", 22, 3.3);
        student st2 = new student("Alex", "White", 19, 2.8);
        student st3 = new student("Jordan", "Anderson", 22, 3.1);

        student[] studentArray;
        studentArray = new student[3];

        studentArray[0] = st1;
        studentArray[1] = st2;
        studentArray[2] = st3;

        student min = studentArray[0], max = studentArray[0], secondMax = studentArray[0];
        if (studentArray[0].gpa > studentArray[1].gpa) {
            secondMax = studentArray[1];
        }
        for (student s : studentArray) {
            System.out.println("Name: " + s.firstName + " " + s.lastName);
            System.out.println("Age: " + s.age);
            System.out.println("GPA: " + s.gpa + "\n");
            if (s.gpa > max.gpa) {
                secondMax = max;
                max = s;
            } else if (s.gpa > secondMax.gpa && s.gpa != max.gpa) {
                secondMax = s;
            }
            if (s.gpa < min.gpa) min = s;
        }
        System.out.println(min.firstName + " has the minimum GPA of " + min.gpa);
        System.out.println(max.firstName + " has the maximum GPA of " + max.gpa);
        System.out.println(secondMax.firstName + " has the second maximum GPA of " + secondMax.gpa);
    }
}

Related Solutions

I Have posted my Java code below. Fix the toString, add, and remove implementations so that...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that the following test cases work. Note: I have removed all the unnecessary inherited List implementations. I have them to: throw new UnsupportedException(); For compilation, you could also add //TODO. Test (Main) List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size...
in java: In my code at have my last two methods that I cannot exactly figure...
in java: In my code at have my last two methods that I cannot exactly figure out how to execute. I have too convert a Roman number to its decimal form for the case 3 switch. The two methods I cannot figure out are the public static int valueOf(int numeral) and public static int convertRomanNumber(int total, int length, String numeral). This is what my code looks like so far: public static void main(String[] args) { // TODO Auto-generated method stub...
Why is this java code reaching a deadlock and how to I fix it without serializing...
Why is this java code reaching a deadlock and how to I fix it without serializing the code (i.e. I don't want to do not have the Girl authenticate the Boy then have the Boy authenticate the Girl)? import java.applet.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; // Attempt at a simple handshake. Girl pings Boy, gets confirmation. // Then Boy pings girl, get confirmation. class Monitor { String name; public Monitor (String name) { this.name = name; } public String...
In Java please. I put down my code and what I was able to achieve so...
In Java please. I put down my code and what I was able to achieve so far: public class Animal {   private String gender; //stores the gender of the animal    private String type; //stores the type of the animal(bear of fish)    private int strength; //stores the strength of the animal    public Animal() {        gender = "none";        type = "none";        strength = 0;    }        public Animal (String g, String...
Hello I have this error in the code, I do not know how to fix it....
Hello I have this error in the code, I do not know how to fix it. It is written in C++ using a Eclipse IDE Error: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string bus.h =========== #pragma once #include using namespace std; class Bus { private:    string BusId; // bus ID    string Manufacturer; // manufacturer of the bus    int BusCapacity; // bus capacity    int Mileage; // mileage of bus    char Status; // current status...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public Fraction () {    numerator = 0;    denominator = 1;    numberOfFraction++; }    public Fraction (int n, int d) {    numerator = n;    denominator = d;    numberOfFraction++; } private int gcd (int num1, int num2) {    if (num1 == 0)    return num2;    return gcd (num2 % num1, num1); }    public Fraction add (Fraction third) {    int n = numerator * third.denominator + third.numerator * denominator;    int...
Python I want to name my hero and my alien in my code how do I...
Python I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name import random class Hero:     def __init__(self,ammo,health):         self.ammo=ammo         self.health=health     def blast(self):         print("The Hero blasts an Alien!")         if self.ammo>0:             self.ammo-=1             return True         else:             print("Oh no! Hero is out of ammo.")             return False     def...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
How do I fix the "error: bad operand types for binary operator '*' " in my...
How do I fix the "error: bad operand types for binary operator '*' " in my code? What I am trying to do: double TotalPrice = TicketPrice * NoOfTickets;       My code: import javax.swing.*; /*provides interfaces and classes for different events by AWT components*/ import java.awt.event.*; import javax.swing.JOptionPane; //TicketReservation.java class TicketReservation { public static void main(String args[]) { /*Declare JFrame for place controls.*/ JFrame f= new JFrame("Movie Ticket Reservation");                                   /*Declare JLabels*/ JLabel...
How would I make it so that when I run my code it does not ask...
How would I make it so that when I run my code it does not ask for input (not having to enter after the statement and enter 0 for example) after ROXY (Forever ROXY Enterprises) appears? Like it would tell me the second statement right away along with the Roxy phrase. This is in C++. My code: #include / #include using std::endl; int main() {    void readAndConvert();    unsigned int stockSymbol;    unsigned int confNum;    std::cout << "ROXY...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT