Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Part 1 : Student.java Define your Student class to meet the following criteria: 3 fields, all private: String name, double [ ] grades, and int
Part : Student.java
Define your Student class to meet the following criteria:
fields, all private: String name, double grades, and int year.
Nondefault constructor that sets values for name, year, and grades.
A public nonstatic method getName which returns the students name.
A private nonstatic method calculateAverage which uses a for loop over the students grades to determine and return their average. If the student has no grades ie the length of the array is return as the average instead.
A public nonstatic method canGraduate that returns true if the students year is and their average is use calculateAverage
Depending on how you style your code, the length of this class will vary my version is just over lines long, for comparisons sake. Theres not any majorly complicated logic in here, its just an exercise in creating a class.
Part : StudentDemo.java
StudentDemo should contain methods. main makeStudent and graduateCheck This part of the lab is focused creating objects of a custom class, and utilizing methods to better organize your program and reduce repetition.
Start with graduateCheck It should be defined as public static void and it should accept a single Student object as parameter. All the method needs to do is print can graduate, congratulations! if their canGraduate method returns true, otherwise print cant graduate, oh well... Youll need to use getName for each student here.
Next, do makeStudent It should be defined as public static and return a Student object. It should accept a Scanner object as parameter. Start by asking for the necessary information to create a Student their name and year first. Then, ask for how many grades they have, and create a double with that size. You can then use a for loop to fill up the array with grades entered by the user. After all that, return the new Student using the information given. Youll also need to flush the stream right before the return since the method ends with nextDouble and starts with nextLine For the grade loop, it should look something like this:
forint i ; i numOfGrades; i
ask for a grade to add to the array
gradesi that new grade
Lastly, you have main which will serve as the main program flow. Create a Scanner object and ask for how many students there are. Use a for loop to get Student objects from makeStudent and call graduateCheck on each after. Youll need to flush the stream after asking for the number of students and before calling makeStudent the first time. Your for loop should look something like this:
forint i ; i studentNum; i
retrieve a new student object from makeStudent
call graduateCheck with that student
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started