Question

In: Computer Science

Lab 3 Java Classes and Memory Management Multi-file Project In this lab you will gain experience...

Lab 3 Java Classes and Memory Management

Multi-file Project

  1. In this lab you will gain experience using classes and arrays of classes. Use the following as the beginning of a student abstract data type.

public class Student

{

private String fName ;

private String lName ;

private double[] grades;

}

This class should be in the package com.csc241. Write a program that prompts a user for a total number of students and dynamically allocates memory for just that number of Student objects in a Student[]. Then continuously re-prompt the user to enter, on a studentbystudent basis,

  1. a student’s first and last name to be used in populating the class data members fName and lName with appropriate set methods. Use local String variables to hold the values entered by the user.
  2. the number of grades for the student to be stored in nGrds, a local variable of the main program.

Next,

  1. dynamically allocate memory for the grades and assign the resulting reference to a local double[]. Prompt the user for the nGrds values and after acquiring them all, assign the local grades array to the Student instance in the array using an appropriate set method.
  2. re-use the MaxMin class you wrote in a previous lab, but also put it in the package com.csc241. Similarly, the GradeCalculator class that you created in the last assignment needs to be in the com.csc241 package. You will be using the avg and maxMin static methods that accept the grades[] in order to compute individual student statistics.
  3. In order for you to calculate ensemble statistics, that is statistics for the entire class, the GradeCalculator class will need methods avg and maxMin that accept as an argument the Student[] that you dynamically allocated. These methods must loop through the individual grades[] of each Student array element in order to compute the total class average, max and min values.
  4. display the results to the user, both on a per student and ensemble basis. Your output should appear as shown below. Please make note of and reproduce all the decimal formatting and other characteristics of the sample output.

As always, keep in mind all of the general rules of good program construction.

How many students are in your class - 3

Enter the first name of student #1 - Joe

Enter Mike’s last name - Shmo

How many grades will you be entering for Joe Shmo? – 5

Plz enter the grades for Joe Shmo

100

97

87

87

92

Grade Statistics for Joe Shmo (average=92.6 ; max/min=100/87)

Enter the first name of student #2 - Jack

Enter Jack’s last name - Schwartz

How many grades will you be entering for Jack Schwartz? – 3

Plz enter the grades for Jack Schwartz

100

90

0

Grade Statistics for Jack Schwartz (average=63.3 ; max/min=100/0)

Enter the first name of student #3 - Joan

Enter Joan’s last name - Jameson

How many grades will you be entering for Joan Jameson? – 4

Plz enter the grades for Joan Jameson

98

95

93

94

Grade Statistics for Joan Jameson (average=95.0 ; max/min=98/93)

Ensemble Statistics - Average=86.1 ; Max/Min=100/0)

Do you wish to continue (Y/N) - n

Solutions

Expert Solution

//Java code

package com.csc241;

public class Student

{

    private String fName ;

    private String lName ;

    private double[] grades;

    //Constructor
    public Student()
    {
        fName = "None";
        lName ="None";
        grades = null;
    }
    //Overload Constructor


    public Student(String fName, String lName, double[] grades) {
        this.fName = fName;
        this.lName = lName;
        this.grades = grades;
    }
    //getters and setters

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public double[] getGrades() {
        return grades;
    }

    public void setGrades(double[] grades) {
        this.grades = grades;
    }
    public double getAverage()
    {
        double sum =0;
        for (int i = 0; i <grades.length ; i++) {
            sum += grades[i];
        }
        double avg = sum/grades.length;
        return avg;
    }
    public double getMax()
    {
        double  max = grades[0];
        for (int i = 0; i <grades.length ; i++) {
            if(max<grades[i])
                max = grades[i];
        }
        return max;
    }
    public double getMin()
    {
        double  min = grades[0];
        for (int i = 0; i <grades.length ; i++) {
            if(min>grades[i])
                min = grades[i];
        }
        return min;
    }


    @Override
    public String toString() {
        return "Grade Statistics for "+fName+" "+lName+"(average = "+String.format("%.1f",getAverage())+"; max/min = "+String.format("%.0f",getMax())+"/"+String.format("%.0f",getMin())+")";
    }

}

//==================================

import com.csc241.Student;

import java.util.Scanner;

public class Main {
    public static void main(String[] args)
    {
        //Create student object
        Student student = new Student();
        //For user input
        Scanner input = new Scanner(System.in);

        Student[] students;
        System.out.print("How many students are in your class: ");
        int numberOfStudents = input.nextInt();
        students = new Student[numberOfStudents];

        for (int i = 0; i <students.length ; i++) {
            System.out.print("Enter the first name of student #"+(i+1)+": ");
            student.setfName(input.next());
            System.out.print("Enter "+student.getfName()+"'s last name: ");
            student.setlName(input.next());
            System.out.print("How many grades will you be entering for "+student.getfName()+" "+student.getlName()+"? ");
            int gradeSize = input.nextInt();
            double[] grades = new double[gradeSize];
            System.out.print("Plz enter the grades for "+student.getfName()+" "+student.getlName()+"\n");
            for (int j = 0; j <grades.length ; j++) {
                grades[j] = input.nextDouble();
            }
            student.setGrades(grades);
            students[i] = student;
            System.out.println(students[i]);
        }
    }
}

//Output

//If you need any help regarding this solution .......... please leave a comment ...... thanks


Related Solutions

Objective The objective of this lab exercise will be for the student to gain experience conducting...
Objective The objective of this lab exercise will be for the student to gain experience conducting a wireless networking site survey. The objective is for each group to propose a wireless networking solution for a given space (such as a classroom, office rooms, home, etc.). Verify that you can establish link (by pinging) with another laptop in your network. (Also, you may demonstrate this by downloading the shared file on another computer in the same network). Required Each individual/group will...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign4Test.java. Copy your code from Assignment 3 into the Student.java and StudentList.java. Assign4Test.java should contain the main method. In Student.java, add a new private variable of type Integer called graduationYear. In Student.java, create a new public setter method setGraduationYear to set the graduationYear. The method will have one parameter of type Integer. The method will set...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the integers SORTED USING RADIX SORT. You may assume all your input consists of integers <=9999. Your main program will input the integers and put them into a QUEUE. It will then pass this queue to a method called radixSort which will sort the numbers in the queue, passing the sorted queue back to main. The main program will then call another method to print...
Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in...
Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and should add...
JAVA Practice Exam 2 The project practice_exam_2 contains three (3) classes: Testing – This is the...
JAVA Practice Exam 2 The project practice_exam_2 contains three (3) classes: Testing – This is the file you will use to execute your program and test your cases. Each section refers to one or more specific array(s). From here, you can run the whole program or one section at a time. TestCases – This file contains all test cases. NO NEED FOR YOU TO PAY ATTENTION TO IT. YourCode – This is where you will be writing your code. Implement...
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
Lab Project 3: Lab Safety
Lab Project 3: Lab Safety 1. List the items of proper lab dress. 2. Describe a how you would use your cell phone in the microbiology lab. 3. List the safety equipment found in labs. 4. Identify the reason you would not use carbon dioxide extinguisher on a person 5. List personal habits that should be avoided in the lab.
Project 3 Details The purpose of this project is to give you experience with creating and...
Project 3 Details The purpose of this project is to give you experience with creating and using custom objects. In it, you will make a couple custom classes that work in tandem with each other, and call them in your main function. For this project we'll be making something kind of like the Chatbot java file we made in class. You will create a Chatbot class that will contain a number of variables and functions. As you can imagine, a...
Using a minimum of 2 classes create a java program that writes data to a file...
Using a minimum of 2 classes create a java program that writes data to a file when stopped and reads data from a file when started. The data should be in a readable format and the program should work in a way that stopping and starting is irrelevant (e.g. all data doesn't have to save just the important elements.) Program should be unique and semi-complex in some way.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT