Question

In: Computer Science

CompSci 251: Intermediate Computer ProgrammingLab 4 – 2019 Introduction For this lab we will be looking...

CompSci 251: Intermediate Computer ProgrammingLab 4 – 2019

Introduction

For this lab we will be looking at static, or class variables. Remember, instance variables are associated with a particular instance, where static variables belong to the class. If there are n instances of a class, there are n copies of an instance variable, but exactly one copy of a static variable. Underlined items are static members of the class.

The lab also uses enums to control what is an acceptable major. Enums are a special kind of class in Java that represent constant values. Enums can also have methods, which I have overridden the toString method. You do not need to change the enum CollegeMajor class. Look at the driver for how they are accessed. Also, they can be compared against other enums as if they were primitive types.

What you need to do

  •  Create a Student class conforming to the diagram above.

  •  Each student has a name, GPA, and major. Assume once a name is chosen it can never be

    changed. This means no setter is required for name.

  •  The class keeps track of how many students have been created and their combined GPA. These

    values are used for getStudentAvg(), which computes the average for all students.

  •  The class also manages the total number of CompSci students and their total GPA. These values

    should only be affected by CompSci majors and are used for getCompSciAvg(). Take note,

    CompSci majors are still considered part of all students.

  •  The setGPA sets the GPA to be between 0.0 and 4.0. When changing a student’s GPA, it must

    accurately update the totalGPA, and possibly totalCompSciGPA, static variable. If the value is not

    in range, nothing happens.

  •  The equals method returns true if all instance variables are equal.

  •  The toString() should return a String in the format.

o Student Name: <name>, Major: <major>, GPA: <gpa>
 A simple driver is provided. When complete your code should look like below.

Driver Output

All students:
Student name: Eva major: Biology GPA: 3.5
Student name: Taiyu major: Computer Science GPA: 4.0 Student name: Shaina major: Civil Engineer GPA: 3.5 Student name: Megan major: Accounting GPA: 3.0
Student name: Antonio major: Computer Science GPA: 3.0 Student name: Jim major: Chemistry GPA: 1.5
Student name: Morgan major: Computer Science GPA: 3.5

Testing equals methods.
Does Eva equal Taiyu?
They are not the same.
Does Antonio equal Antonio?
They are the same.
Testing static variables.
All Students avg = 3.14
All CompSci Students avg = 3.50

Time to set all gpa values to 3.0.
If done correctly, both averages should be 3.0.

All Students avg = 3.00
All CompSci Students avg = 3.00

Time to set all gpa values of CompSci Students to 4.0. If done correctly, both averages should be different.

All Students avg = 3.43
All CompSci Students avg = 4.00

given code

public enum CollegeMajor {

   COMPSCI,BIOLOGY,NURSING,ACCOUNTING,ARCHITECTURE,FINANCE,CHEMISTRY,CIVILENGINEERING;

   @Override
   public String toString() {
       switch(this) {
       case COMPSCI:
           return "Computer Science";
       case BIOLOGY:
           return "Biology";
       case NURSING:
           return "Nursing";
       case ACCOUNTING:
           return "Accounting";
       case ARCHITECTURE:
           return "Architecture and Interior Design";
       case FINANCE:
           return "Finance";
       case CHEMISTRY:
           return "Chemistry";
       case CIVILENGINEERING:
           return "Civil Engineer";
       default:
           return "";
       }
   }

}

ublic class Driver {

   public static void main(String[] args) {

       Student[] students = new Student[7];

       students[0] = new Student("Eva", 3.5, CollegeMajor.BIOLOGY);
       students[1] = new Student("Taiyu", 4.0, CollegeMajor.COMPSCI);
       students[2] = new Student("Shaina", 3.5, CollegeMajor.CIVILENGINEERING);
       students[3] = new Student("Megan", 3.0, CollegeMajor.ACCOUNTING);
       students[4] = new Student("Antonio", 3.0, CollegeMajor.COMPSCI);
       students[5] = new Student("Jim", 1.5, CollegeMajor.CHEMISTRY);
       students[6] = new Student("Morgan", 3.5, CollegeMajor.COMPSCI);

       System.out.println("All students:");
      
       for(Student s: students) {
           System.out.println(s);
       }
      
       System.out.println("\n\nTesting equals methods.\n");
      
       System.out.println("Does " + students[0].getName() + " equal " + students[1].getName() + "?");  
       if(students[0].equals(students[1])) {
           System.out.println("They are the same.");
       } else {
           System.out.println("They are not the same.");
       }
      
       System.out.println("\nDoes " + students[4].getName() + " equal " + students[4].getName() + "?");  
       if(students[4].equals(students[4])) {
           System.out.println("They are the same.");
       } else {
           System.out.println("They are not the same.");
       }
      
       System.out.println("\n\nTesting static variables.\n");
       System.out.printf("All Students avg = %.2f\n", Student.getStudentAvg());
       System.out.printf("All CompSci Students avg = %.2f\n", Student.getCompSciAvg());

       System.out.println("\nTime to set all gpa values to 3.0.");
       System.out.println("If done correctly, both averages should be 3.0.\n");

       for(Student s: students) {
           s.setGPA(3.0);
       }

       System.out.printf("All Students avg = %.2f\n", Student.getStudentAvg());
       System.out.printf("All CompSci Students avg = %.2f\n", Student.getCompSciAvg());

       System.out.println("\nTime to set all gpa values of CompSci Students to 4.0.");
       System.out.println("If done correctly, both averages should be different.\n");

       for(Student s: students) {
           if(s.getMajor()==CollegeMajor.COMPSCI)
               s.setGPA(4.0);
       }

       System.out.printf("All Students avg = %.2f\n", Student.getStudentAvg());
       System.out.printf("All CompSci Students avg = %.2f\n", Student.getCompSciAvg());

   }

}

Solutions

Expert Solution


// Student.java
        class Student {
                private String name;
                private double gpa;
                private CollegeMajor major;

                private static double totalGpa = 0;
                private static int studentsCount = 0;
                private static double totalCompSciGpa = 0;
                private static int studentsCountCompSci = 0;
                
                public Student(String name, double gpa, CollegeMajor major) {
                        this.name = name;
                        this.gpa = gpa;
                        this.major = major;
                        totalGpa += gpa;
                        studentsCount++;
                        
                        if(major.equals(CollegeMajor.COMPSCI)) {
                                totalCompSciGpa += gpa;
                                studentsCountCompSci++;
                        }
                }

                public String getName() {
                        return name;
                }

                public double getGpa() {
                        return gpa;
                }

                public CollegeMajor getMajor() {
                        return major;
                }

                public static double getStudentAvg() {
                        if(studentsCount == 0) {
                                return 0;
                        }
                        return totalGpa/studentsCount;
                }

                public static double getCompSciAvg() {
                        if(studentsCountCompSci == 0) {
                                return 0;
                        }
                        return totalCompSciGpa/studentsCountCompSci;
                }

                public void setGPA(double d) {
                        if(d >= 0 && d <= 4) {
                                
                                if(major.equals(CollegeMajor.COMPSCI)) {
                                        totalCompSciGpa -= gpa;
                                        totalCompSciGpa += d;
                                }
                                totalGpa -= gpa;
                                totalGpa += d;
                                
                                gpa = d;
                        }
                }

                @Override
                public String toString() {
                        return "Student Name: " + name + ", Major: " + major + ", GPA: " + gpa;
                }

                @Override
                public boolean equals(Object obj) {
                        if (this == obj)
                                return true;
                        if (obj == null)
                                return false;
                        if (getClass() != obj.getClass())
                                return false;
                        Student other = (Student) obj;
                        if (Double.doubleToLongBits(gpa) != Double.doubleToLongBits(other.gpa))
                                return false;
                        if (major != other.major)
                                return false;
                        if (name == null) {
                                if (other.name != null)
                                        return false;
                        } else if (!name.equals(other.name))
                                return false;
                        return true;
                }
        }
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

CompSci 251 Assignment 1 Due Sep. 23rd, 2019 Create a program that will format long pieces...
CompSci 251 Assignment 1 Due Sep. 23rd, 2019 Create a program that will format long pieces of text based on user input. User input will determine the number of indents allowed for the first line in each paragraph, the number of characters allowed per line, and the number of sentences allowed per paragraph. There are three pieces of text, where each is a famous speech that is represented by a single String variable at the top of the base file....
COMP 251: Data Structures and Algorithms – Lab 4 Page 1 of 2 Lab 4: Implementing...
COMP 251: Data Structures and Algorithms – Lab 4 Page 1 of 2 Lab 4: Implementing a Stack Using Linked List Implementation Objectives: The aim of this lab session is to make you familiar with using the linked list implementation in week3 and implement a stack using it. In this lab you will complete the partially implemented StackUsingLinkedList. Specifically, you will implement the following methods. • The default constructor: public StackUsingLinkedList() • public void push(AnyType x) • public AnyType pop()...
Introduction In this lab, we will look at various aspects of methods in Java programming such...
Introduction In this lab, we will look at various aspects of methods in Java programming such as passing arguments to a method, use of local variables, and returning a value from a method. Exercise-1: RainFall Statistisc(50 pts) Write a program that asks user the total rainfall for the last six months of two consecutive years and do the following: the total rainfall per year the average monthly rainfall (for two years) the month with the most rain (per year) You...
In an organic chemistry lab we were looking at the substrate and nucleophile effects in Sn2...
In an organic chemistry lab we were looking at the substrate and nucleophile effects in Sn2 reactions. Our experimental data showed that 1-bromobutane with 1.0mL of 0.25M NaI in 2-Butanone reacted faster than 1-bromobutane with 1.0mL 0.50M NaI in 2-butanone. Also, our experimental data showed that 1-bromobutane with 1.0mL of 0.50M NaI reacted faster than 1-bromobutane with 2.0mL of 0.05M NaI. These result are the opposite of the theoritcal rate aren't they? From my understanding, the Sn2 reaction rate should...
We are doing a lab on Grignard Reactions, i'm just looking to verify the system by...
We are doing a lab on Grignard Reactions, i'm just looking to verify the system by which to find this information: Add about 2 mmol Mg powder, recording the mass to the nearest milligram. In this experiment, magnesium will be the limiting reactant because a 5% excess of bromobenzene will be used. Using your mass of Mg, determine the mass of bromobenzene to be used, being sure to calculate a 5% molar excess.
Suppose we need to connect Computer Network Research Lab (CNRL) network with address 167.89.0.0 through a...
Suppose we need to connect Computer Network Research Lab (CNRL) network with address 167.89.0.0 through a router to the EE Department network with address 123.0.0.0 and connect CNRL network through a router to the EE Research network with address 222.22.2.0 . Please draw a diagram for this network connection. Choose IP addresses for each interface of each router and show several hosts on each network, with their IP addresses. Please briefly explain each step.
During lab 4, we have seen numerical implementation of Fourier Series for periodic signals. As first...
During lab 4, we have seen numerical implementation of Fourier Series for periodic signals. As first part of this assignment, you need to write a Matlab function that would take an array representing a single period of a signal (x), corresponding time array (t), and return the Fourier Series coefficients (Ck) in exponential form. The function should also be able to take two (2) optional input arguments: number of Fourier coefficients (Nk) and plot option (p). Use the template ‘fourier_series_exp.m’...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT