Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

8.*********Help With Java, So i finished my previous lab which is called LAB 7. but having problems with lab 8. Please explain and show OUTPUT.

8.*********Help With Java, So i finished my previous lab which is called LAB 7. but having problems with lab 8. Please explain and show OUTPUT.

THIS IS THE CODE FROM LAB 7:

import java.util.Arrays; import java.lang.*; import java.io.*;

class Student{

//Data members String name; int quizzes = 0; int count = 0;

//Constructor Student Student(String n){ name = n; }

//methods public String getName(){ return name; }

public void addQuiz(int score){ quizzes = quizzes + score; count++; }

public int getTotalScore(){ return quizzes; }

public int getAverageScore(){ int avg = quizzes/count; return avg; }

public int getHighestScore(int[] q){

int[] qz = getScores(q); return qz[qz.length-1]; }

public int getLowestScore(int[] q){

int[] qz = getScores(q); return qz[0]; }

public int[] getScores(int[] q){

for(int i=0;i for(int j=i+1;j

if(q[i]>q[j]){ int temp = q[i]; q[i] = q[j]; q[j] = temp; } }} return q; }

public String toString(int[] q){ return " Name : "+name+" , Number of quizzes : "+count; } }

1.Create a project for Lab 8. Copy the Student class (developed in Lab 7) to Lab 8. Make the following modifications to the Student class: [You may want to make a change and then test it before moving on to the next change. See Step 3.]

a. Add instance variables to store a first name and last name.

b. Add a constructor the take a first name and last name as parameters

c. Add a method to get the first name.

d. Add a method to return the last name

e. What do we do with the name instance variable?

i. Delete the name instance variable.

ii. Modify getName() to return the concatenation of the first name and last name separated by a space character.

iii. Modify the constructor that has the name parameter to extract the first name and last name from the name parameter. The first name and last name are separated by at least one space character. There may be leading or trailing space characters. The last name may be missing. There are several approaches to extracting the names, i.e. String methods or Scanner.

iv. Modify toString() to include the first name and last name.

2. Copy the Lab7StudentTester class to the Lab 8 project. Rename it to Lab8StudentTester. Modify the Lab8StudentTester to test the new methods and constructor. Test the modified methods and constructor [you should not need to modify the tester since the method and constructor signatures did not change].

3. Modify the Student class to implement the Comparable interface. [Hint: you will implement the compareTo method]. The natural ordering for the Student class is last name followed by first name. For example:

- Bill Johnson is less than Bill Smith

- Bill Thomas is greater than Bill Smith

- Bill Smith is equal to Bill Smith

- Betty Johnson is less than Bill Johnson

- Bob Johnson is greater than Bill Johnson

4. Modify the Lab8StudentTester to test the compareTo method.

5. Copy Lab8StudentSorter.java into the Lab 8 project. Run the Lab8StudentSorter file. The expected output is shown below. If your output does not match the expected output, then your compareTo method may have some problems that were not detected in Step 4. Revisit Steps 3 and 4

image text in transcribed

HERE THE CODE FOR LAB 8:

import java.util.Arrays; /** * Sort an array of Student Objects. The sort requires that the Student * class implement the Comparable interface. * */ public class Lab8StudentSorter { public static void main(String[] args) { Student[] studentData = { new Student("Bill", "Johnson"), new Student("Tom", "Thompson"), new Student("Penny", "King"), new Student("Bill", "Thomas"), new Student("Bob", "Johnson"), new Student("Betty", "Johnson"), new Student("Bill", "Smith"), new Student("Sam", "Appleton"), new Student("Sue", "Cook"), }; System.out.println("Unsorted Student Data"); for(Student student : studentData) { System.out.println(student); } // Sort the student data using the natural ordering Arrays.sort(studentData); System.out.println(); System.out.println(); System.out.println("Sorted Student Data"); for(Student student : studentData) { System.out.println(student); } } } 
run Unsorted Student Data Bill Johnson Tom Thompson Penny King Bill Thomas Bob Johnson Betty Johnson Bill Smith Sam Appleton Sue Cook Sorted Student Data Sam Appleton Sue Cook Betty Johnson Bill Johnson Bob Johnson Penny King Bill Smith Bill Thomas Tom Thompson BUILD SUCCESSFUL (total time 0 seconds)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is dividend payout ratio ?

Answered: 1 week ago

Question

Explain the factors affecting dividend policy in detail.

Answered: 1 week ago

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago