We are given the Java class Student, class Randomizer, and class Classroom.
We were told to fill in the method for getTopStudent and getAverageScore.
Below I have included the classes for Student, Classroom, and ClassroomTester.
The problem: when I run it, the compiler errors.
And I don't get why it's wrong...I did not forget a semicolon and I am not sure what they meant by "illegal start of expression"
public class Student
{
private static final int NUM_EXAMS = 4;
private String firstName;
private String lastName;
private int gradeLevel;
private double gpa;
private int[] exams;
private int numExamsTaken;
public Student(String fName, String lName, int grade)
{
firstName = fName;
lastName = lName;
gradeLevel = grade;
exams = new int[NUM_EXAMS];
numExamsTaken = 0;
}
public double getAverageScore()
{
int sum = 0;
for(int i = 0; i < exams.length;i++)
{
sum+=exams[i];
}
return (double)sum/numExamsTaken;
}
public String getName()
{
return firstName + " " + lastName;
}
public void addExamScore(int score)
{
exams[numExamsTaken] = score;
numExamsTaken++;
}
// This is a setter method to set the GPA for the Student.
public void setGPA(double theGPA)
{
gpa = theGPA;
}
public String toString()
{
return firstName + " " + lastName + " is in grade: " + gradeLevel;
}
}
public class Classroom
{
Student[] students;
int numStudentsAdded;
public Classroom(int numStudents)
{
students = new Student[numStudents];
public Student getTopStudent()
{
double max = students[0].getAverageScore();
String topstudent = students[0].getName();
for (int i = 0; i < students.length; i++)
{
if (students[i].getAverageScore() > max)
{
max = students[i].getAverageScore();
topstudent = students[i];
}
}
return topstudent;
}
public void addStudent(Student s)
{
students[numStudentsAdded] = s;
numStudentsAdded++;
}
public void printStudents()
{
for(int i = 0; i < numStudentsAdded; i++)
{
System.out.println(students[i]);
}
}
}
public class ClassroomTester extends ConsoleProgram
{
public void run()
{
Classroom c = new Classroom(2);
Student ada = new Student("Ada", "Lovelace", 12);
ada.addExamScore(44);
ada.addExamScore(65);
ada.addExamScore(77);
Student alan = new Student("Alan", "Turing", 11);
alan.addExamScore(38);
alan.addExamScore(24);
alan.addExamScore(31);
// add students to classroom
c.addStudent(ada);
c.addStudent(alan);
c.printStudents();
Student topStudent = c.getTopStudent();
System.out.println(topStudent);
}
}
Change the constructor of Classroom to
public Classroom(int numStudents)
{
students = new Student[numStudents];
}
You are missing the closing brace } on the constructor of Classroom.
You are missing a closing {
public Classroom(int numStudents)
{
students = new Student[numStudents];
Related
The question wants me to do:
An array of Finance called financeRecord to store the details
of the payments for each semester.
This is my code
package lab5;
class Student_U extends Student {
public String student_name;
private String studentID;
public int student_age;
private byte currentSemester;
private byte TotalFinanceRecord;
private String cohort;
public Student_U() {
student_name = " ";
studentID = " ";
student_age = 0;
currentSemester = 1;
TotalFinanceRecord = 0;
cohort = " ";
}
public Student_U(String student_name, String studentID, int student_age,
String course, String year,
String section, String subject, String student_name2,
String studentID2, int student_age2,
byte currentSemester, byte totalFinanceRecord, String cohort) {
super(student_name, studentID, student_age, course, year,
section, subject);
student_name = student_name2;
studentID = studentID2;
student_age = student_age2;
this.currentSemester = currentSemester;
TotalFinanceRecord = totalFinanceRecord;
this.cohort = cohort;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public int getStudent_age() {
return student_age;
}
public void setStudent_age(int student_age) {
this.student_age = student_age;
}
public byte getCurrentSemester() {
return currentSemester;
}
public void setCurrentSemester(byte currentSemester) {
this.currentSemester = currentSemester;
}
public byte getTotalFinanceRecord() {
return TotalFinanceRecord;
}
public void setTotalFinanceRecord(byte totalFinanceRecord) {
TotalFinanceRecord = totalFinanceRecord;
}
public String getCohort() {
return cohort;
}
public void setCohort(String cohort) {
this.cohort = cohort;
}
public void initStudent() {
}
public void print() {
System.out.print("Student name: " + student_name + " ");
System.out.print("\nMatric No: " + studentID + " ");
System.out.print("\nAge: " + student_age + " ");
System.out.print("\nCurrent Semester: " + currentSemester + " ");
System.out.print("\nCohort: " + cohort + " ");
System.out.println();
}
}
Please help me fix my code I would appreciate it so much.
This is my lab assignment which needs to be submitted by tomorrow.
You could try this, but it's also better to review standard java concepts (arrays, classes, etc). After, just adapt your code as suitable.
public class Finance extends Student
{
public static void main(String args[])
{
Finance f1 = new Finance("Student_1");
System.out.println(f1);
f1.setPayment(1, 10);
System.out.println(f1);
f1.setPayment(2, 10.77);
System.out.println(f1);
Student s2 = new Student("Student 2");
Finance f2 = new Finance(s2);
f2.setPayment(2, 88.77);
System.out.println(f2);
}
Double finaceRecord[] = new Double[3];
private void initPayment()
{
for(int i=0;i<finaceRecord.length;i++)
{
finaceRecord[i]=0.0;
}
}
public Finance(Student s)
{
super(s.name);
initPayment();
}
public Finance(String name)
{
super(name);
initPayment();
}
//store first or second
public void setPayment(int i, double d)
{
if(d<=0) return;
if(i==1)
{
finaceRecord[i] = d;
}
else
{
finaceRecord[2] = d;
}
finaceRecord[0] = finaceRecord[2] + finaceRecord[1];
}
public String toString()
{
return "name="+super.name+", Total Paid="+finaceRecord[0]+","
+ " Sem1="+finaceRecord[1]+", Sem2="+finaceRecord[2];
}
}
...
public class Student
{
String name;
int Semester;
Student(String name)
{
this.name = name;
this.Semester = 1;
}
}
Ouptut
name=Student_1, Total Paid=0.0, Sem1=0.0, Sem2=0.0
name=Student_1, Total Paid=10.0, Sem1=10.0, Sem2=0.0
name=Student_1, Total Paid=20.77, Sem1=10.0, Sem2=10.77
name=Student 2, Total Paid=88.77, Sem1=0.0, Sem2=88.77
from what I understand you are supposed to create an array of Finance type
Finance []financeRecord = new Finance[totalFinanceRecord];
and then you can access the values of Finance class
financeRecord[indexNumber].methodName();
I am trying to complete a for loop where I reference the method getCredits() from the UAClass class. I continue to get an error that states that the symbol cannot be found and it points to the ".getNumCredits" portion of the code in line 68 and 73. I am relatively new to java but I made sure to check my spelling, etc. Any help would be appreciated!
I checked the spelling and capitalization to make sure the reference in the for loop matched the method in the other class.
public class Student{
private String firstName;
private String lastName;
private String departmentIn;
private int yearGraduation;
private double gpa;
private double[] gradeReceived = {4.0, 3.0, 3.0, 2.0, 4.0};
private String[] classList = {"history", "biology", "physics", "english", "finance"};
private static double totalClassPoints;
private static int totalCredits;
public Student(String firstName, String lastName, String departmentIn, int yearGraduation) {
this.firstName = firstName;
this.lastName = lastName;
this.departmentIn = departmentIn;
this.yearGraduation = yearGraduation;
}
UAClass history = new UAClass("Bob", "Smith", "Spring", 3);
UAClass biology = new UAClass("Reggie", "Jones", "Fall", 3);
UAClass physics = new UAClass("Carl", "Grimes", "Fall", 4);
UAClass english = new UAClass("Rebecca", "Johnson", "Spring", 3);
UAClass finance = new UAClass("Rachel", "Bomber", "Spring", 3);
public void setStudentFirstName(String StudentFirstName){
StudentFirstName = firstName;
}
public void setStudentLastName(String StudentLastName){
StudentLastName = lastName;
}
public void setDepartmentIn(String DepartmentIn){
DepartmentIn = departmentIn;
}
public void setYearGraduation(int YearGraduation){
YearGraduation = yearGraduation;
}
public String getStudentFirstName(){
return firstName;
}
public String getStudentLastName(){
return lastName;
}
public String getDepartmentIn(){
return departmentIn;
}
public int getYearGraduation(){
return yearGraduation;
}
public double getGPA(){
return gpa;
}
public void calcGPA(){
for(int i=0; i < 5; i++){
totalClassPoints += (gradeReceived[i] * classList[i].getNumCredits());
}
for(int i=0; i < 5; i++){
totalCredits += classList[i].getNumCredits();
}
gpa = totalClassPoints/totalCredits;
System.out.println("\nFirst Name: " + firstName + "\nLast Name: " + lastName + "\nDepartmentIn: " + departmentIn + "\nGraduation Year: " + yearGraduation + "\nGPA: " + gpa);
}
public static void main(String []args){
Student st1 = new Student("Matt", "Watson", "MIS", 2019);
st1.calcGPA();
System.out.println();
}
class UAClass{
public String teacherFirstName;
public String teacherLastName;
public String semesterOffered;
public int numCredits;
public UAClass(String teacherFirstName, String teacherLastName, String semesterOffered, int numCredits){
this.teacherFirstName = teacherFirstName;
this.teacherLastName = teacherLastName;
this.semesterOffered = semesterOffered;
this.numCredits = numCredits;
}
public void setTeacherFirstName(String TeacherFirstName){
TeacherFirstName = teacherFirstName;
}
public void setTeacherLastName(String TeacherLastName){
TeacherLastName = teacherLastName;
}
public void setSemesterOffered(String SemesterOffered){
SemesterOffered = semesterOffered;
}
public void setNumCredits(int NumCredits){
NumCredits = numCredits;
}
public String getTeacherFirstName(){
return teacherFirstName;
}
public String getTeacherLastName(){
return teacherLastName;
}
public String getSemesterOffered(){
return semesterOffered;
}
public int getNumCredits(){
return numCredits;
}
Answer by Nitika in comment:
Here classList[i] is String and to call methods of UAClass class, you need a reference of that class.
I'm having issues trying to figure out the problem with my code. Basically, the program is supposed to be a GPA calculator.
The first part:
import java.io.*;
import java.util.ArrayList;
public class Student {
// Data
private String name;
private int student_id;
private double gpa;
private ArrayList<Integer> grades;
private int num_courses;
// Methods
// Constructor Method
public Student() {
name = "";
student_id = 0;
gpa = 0.0;
grades = new ArrayList<Integer>();
}
// Accessor Methods (getters and setters)
public void setGrade(int g) {
grades.add(g);
calcGPA();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStudent_id() {
return student_id;
}
public void setStudent_id(int student_id) {
this.student_id = student_id;
}
public double getGPA() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public int getNum_courses() {
return num_courses;
}
public void setNum_courses(int num_courses) {
this.num_courses = num_courses;
}
// Functional Methods
public void calcGPA() {
int sum_grades = 0;
for (int i=0; i<this.grades.size(); i++) {
sum_grades = sum_grades * this.grades.get(i);
}
gpa = sum_grades/this.grades.size();
}
public void displayGrades() {
for (int i=0; i<this.grades.size(); i++) {
System.out.println("Grade in course " + i + ": " + this.grades.get(i));
}
}
}
and the second class:
import java.io.*;
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student(); //Object creation/instantiation
s1.setName("John Rodgers");
s1.setStudent_id(111);
s1.setGrade(3);
s1.setGrade(4);
s1.setGrade(3);
System.out.println("Student " + s1.getName() + "\'s GPA: " + s1.getGPA());
Student s2 = new Student();
s2.setName("Jenny Marshall");
s2.setStudent_id(333);
s2.setGrade(4);
s2.setGrade(4);
s2.setGrade(3);
s2.setGrade(4);
s2.setGrade(3);
System.out.println("Student " + s2.getName() + "\'s GPA: " + s2.getGPA());
}
}
The output shows as:
Student John Rodgers's GPA: 0.0
Student Jenny Marshall's GPA: 0.0
The GPA is supposed to be calculated but it appears as 0.0.
Your calcGPA() logic is incorrect (you are doing a product with 0), rather you should sum all grades as shown below:
public void calcGPA(){
int sum_grades = 0;
for(int i=0; i<this.grades.size(); i++){
sum_grades = sum_grades + this.grades.get(i);//sum the grades
}
gpa = sum_grades/this.grades.size();
}
Can anybody explain how I get the highest value from all the objects I have created in my main class?
In this example I have created 2 students objects in my main class and added some course names and grades.
I have made 2 static arrays to store the information from the different objects but it doesn't return the object with the highest grade.
How do I store the highest grade from the student objects created?
public class CMate {
private static String[] studentName = new String[2];
private static int[][] gradeAssigned = new int[2][3];
private static int higGrade = 0;
private String name;
private int cpr;
private String[] courseName = new String[3];
private int[] grade = new int[3];
private int numberOfCourse;
private int numberOfGrades;
private static int counter = 0;
CMate(String name, int cpr) {
// building constructor
System.out.println("Creating object nr " + counter);
this.name = name;
this.cpr = cpr;
// insert name into array
studentName[counter] = name;
for (int i = 0; i < numberOfCourse; i++) {
gradeAssigned[counter][i] = grade[i];
}
counter++;
}
public void addcourseName(String nameOfCourse) {
courseName[numberOfCourse] = nameOfCourse;
numberOfCourse++;
}
public void addGrade(int gradeNr) {
grade[numberOfGrades] = gradeNr;
numberOfGrades++;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCpr() {
return cpr;
}
public void setCpr(int cpr) {
this.cpr = cpr;
}
public String[] getCourseName() {
return courseName;
}
public void setCourseName(String[] courseName) {
this.courseName = courseName;
}
public int[] getGrade() {
return grade;
}
public void setGrade(int[] grade) {
this.grade = grade;
}
public String toString() {
String studentInfo = "";
System.out.println("------------------------------\n student " + name + " CPR " + cpr);
for (int i = 0; i < numberOfCourse; i++) {
studentInfo += " Course " + courseName[i] + "Grade " + grade[i];
}
return studentInfo;
}
public double averageGrade() {
double total = 0;
for (int i = 0; i < numberOfGrades; i++) {
total += grade[i];
}
return (total / numberOfGrades);
}
public void higestGrade() {
for (int i = 0; i < studentName.length; i++) {
if (grade[i] > higGrade) {
higGrade = grade[i];
}
}
}
public void higestObjectGrade() {
System.out.println(higGrade);
}
}
I don't fully understand your code, but here's a simplified version, including a static field to keep all students, and a static function to return the highest grade:
class Student {
public final static List<Student> allCreatedStudents = new ArrayList<Student>();
String name;
int[] grades = new int[3];
public Student(String name){
this.name = name;
allCreatedStudents.add(this); // Every time a student is created, he is recorded in the static list
}
public void setGrade(int grade, int index){
this.grades[index] = grade;
}
public static int getHighestGrade(){
int highestGrade = 0;
for(Student s : allCreatedStudents){ // Loop through all students
for(int i=0; i<s.grades.length; i++){ // Loop through all grades
if(s.grades[i]>highestGrade)
highestGrade = s.grades[i];
}
}
return highestGrade;
}
}
this is the code, from what I understand, it is suppose to work.
Also when I debug my program, it seems that the array is being filled with the objects that the same method is creating.
But still, when I try to print it, it shows me "null" back agian, like the "return" does not work.
Why is it happening?
public class Course {
String courseName;
String teacherName;
int gradeAv;
static int counter;
static int maxNumOfStudents;
Student studentsArray[] = new Student[5];
int numOfStudents = studentsArray.length;
public Course() {
}
public Course(String courseName) {
this();
this.courseName = courseName;
}
public Course(String courseName, String teacherName) {
this(courseName);
this.courseName = courseName;
this.teacherName = teacherName;
}
public Student[] addStudent(String name, int age, int grade) {
for (int i = 0; i < 5; i++) {
studentsArray[i] = new Student(name, age, grade);
age += 10;
grade += 10;
}
return studentsArray;
}
public void printStudentArray(Student studentArray[]) {
studentArray = this.studentsArray;
for (int i = 0; i < studentsArray.length; i++) {
System.out.println(studentsArray[i]);
}
}
public int gradeAv() {
for (int i = 0; i < studentsArray.length; i++) {
int temp = 0;
if (studentsArray[i].grade > temp) {
gradeAv = temp;
System.out.println(gradeAv);
}
}
return gradeAv;
}
public void printCourse() {
System.out.println("Course: ");
System.out.println("Course Name: " + courseName + ". "
+ "Teacher's Name: " + teacherName + ". "
+ "Number Of Students: " + numOfStudents + ". ");
}
}
This is my main class:
public class MainClass {
public static void main(String[] args) {
Student stud = new Student();
Course cour = new Course("Java", "Ronni");
stud.addStudent("Joe", 23, 100);
stud.printStudent();
System.out.println();
stud.printCourse();
System.out.println();
cour.printStudentArray(cour.studentsArray);
System.out.println();
// cour.gradeAv();
}
}
in
Student stud = new Student();
Course cour = new Course("Java", "Ronni");
stud.addStudent("Joe", 23, 100);
stud.addStudent("Joe", 23, 100); will not add any students to your course. stud is an instance of a completely different class, Student, and the implementation of Student is not in the code you've posted.
call the addStudent method of cour instead of the one for stud
cour.addStudent("Joe", 23, 100);
instead
I would also like to add that there are a lot of curious elements to your code, some of which people have brought up in the comments. It would be tedious to list them all. I'd take a look-through, just to make sure you're not doing redundant things.
Try this for example:
Course class:
import java.util.ArrayList;
import java.util.List;
public class Course {
private List<Student> students = new ArrayList<Student>();
private String teacherName;
private String subjectName;
public Course(String subjectName, String teacherName) {
this.subjectName = subjectName;
this.teacherName = teacherName;
}
public void addStudent(Student student) {
students.add(student);
}
public float getAverageGrade() {
float grade = 0;
for (Student student : students) {
grade += student.getGrade();
}
return grade / students.size();
}
public void printCourse() {
System.out.println("Course "+subjectName+" taught by "+teacherName);
System.out.println("Students:");
printStudents();
System.out.println("Aberage grade: "+getAverageGrade());
}
public void printStudents() {
for (Student student : students) {
System.out.println(student.getName()+"\t age "+student.getAge()+" \t grade "+student.getGrade());
}
}
}
Student class:
public class Student {
private String name;
private int age;
private int grade;
public Student(String name, int age, int grade) {
this.grade = grade;
this.age = age;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}