I am having errors doing this question. I have copied the code needed for it and
ID: 3675026 • Letter: I
Question
I am having errors doing this question. I have copied the code needed for it and what I have done but has errors and won’t run on Jgrasp
Create a LastNameFirstInitial.Project6 folder. Reuse the StudentBody.java file. Reuse the
Student.java file. Reuse the Address.java file. Make sure you are in the StudentBody.java file when you compile and run. If you have no run-time errors and your output matches the output in the last page, you are ready to begin making the modifications in the next step.
Modify the Student class as follows:
Each Student object should also contain an array for a schedule (collection) of courses. The instance variable declaration will look something like this: private Course[] schedule;
A Student can register for as many as 6 classes per semester and up to 3 semesters in advance
Modify the constructors so that each Student has a schedule.
e. Modify the toString method so the Student schedule is printed AFTER the student’s name and address. If you are reusing code from Project 5, comment out the code that prints test scores and average. Use a for-loop to print the student’s schedule.
Create a class for a Course that contains the following instance variables (all can be Strings):
Term (e.g., 20151 for Fall 2014, 20152 for Spring 2015, 20152 for Summer 2015)
Course Number (e.g., 368010)
Location - Building and Room Number (e.g., E207, M2102)
Instructor (Lastname, Firstname as one String with a comma in the middle – “Rice, Condoleezza”).
Your Course needs a constructor that sets all instance values based on parameter values.
Use a static variable like count to keep up with the number of courses you create (for all Students).
Your Course class will need four methods to SET the instance variables.
Your Course class will need four methods to GET the instance variables.
Your Course class will need a toString method that prints the Course information in this format (use a tab “ ” between each item):
Term: 20131 Course Num: 368010 Location: South Instructor: Rice, Condoleezza
Modify the StudentBody main method :
Use an array to hold all the Students entered in one session; you can hard code some examples to test your code before perfecting the while loops in the next step.
Use nested while loops to 1) enter students into the studentBody array and 2) enter courses into the student’s schedule.
Display prompts to the user for:
entering a student’s name
entering a student’s addresses (school and home)
adding courses to the student’s schedule
ending data entry for the student; if you are finished entering data for the student, print the student.
ending data entry for all students; if you are finished entering students, print the number of students, print the number of courses for all students (the Course static variable), and the index and student’s name for all students (use a for-loop for the studentBody array).
Output: after the user indicates the end of course entry for a student, print out the student
Output: after the user indicates the end of student entry (no more students), use appropriate labels and:
print the number of students
print the number of courses for all students (the Course static variable)
print the index and the student’s name for all students (use a for-loop for the studentBody array)
STUDENT BODY CODE:
public class StudentBody
{
// Creates some Address and Student objects and prints them
public static void main (String[] args)
{
Address school = new Address ("800 Lancaster Ave." , "Villanova","PA" , 19085);
Address jHome = new Address ("21 Jump Street", "Lynchburg","VA", 24551);
Student john = new Student ("John", "Smith", jHome, school);
Address mHome = new Address ("123 Main Street", "Euclid", "OH", 44132);
Student marsha = new Student ("Marsha", "Jones", mHome, school);
System.out.println (john);
System.out.println();
System.out.println (marsha);
}
}
STUDENT CODE:
public class Student
{
private String firstName, lastName;
private Address homeAddress, schoolAddress;
// Constructor: Sets up this student with the specified values
public Student (String first, String last, Address home, Address school)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
}
// Returs a string description of this student object
public String toString()
{
String result;
result = firstName + " " + lastName + " ";
result+= "Home Address: " + homeAddress + " ";
result += "School Address: " + schoolAddress;
return result;
}
}
ADDRESS CODE:
public class Address
{
private String streetAddress, city, state;
private long zipCode;
// Constructor: Sets up this address with the specified data
public Address (String street, String town, String st, long zip)
{
streetAddress = street;
city = town;
state = st;
zipCode = zip;
}
// Returns a desciption of this address object
public String toString()
{
String result;
result = streetAddress + " ";
result += city + ", " + state + " " + zipCode;
return result;
}
}
Answer: but is not working because I have errors
import java.util.Scanner;
class Course
{
static int count;
String team,courseNumber,location,instructor;
public Course(String team, String courseNumber, String location, String instructor) {
this.team = team;
this.courseNumber = courseNumber;
this.location = location;
setInstructor(instructor);
count++;
}
public String getCourseNumber() {
return courseNumber;
}
public void setCourseNumber(String courseNumber) {
this.courseNumber = courseNumber;
}
public String getInstructor() {
return instructor;
}
public void setInstructor(String instructor) {
this.instructor = instructor;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
public String toString()
{
return team+" "+courseNumber+" "+location+" "+instructor;
}
}
class Student
{
public String firstName, lastName;
private Address homeAddress, schoolAddress;
private Course[] shedule;
// Constructor: Sets up this student with the specified values
public Student (String first, String last, Address home, Address school,Course[] shedule)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
this.shedule = shedule;
}
// Returs a string description of this student object
public String toString()
{
String result;
result = firstName + " " + lastName + " ";
result+= "Home Address: " + homeAddress + " ";
result += "School Address: " + schoolAddress +" ";
result+="Course Details : ";
String temp = "";
for(Course c : shedule)
{
temp+="Team : "+c.team+" Course Number : "+c.courseNumber+" Location : "+c.location+" Instructor Name :"+c.instructor;
temp+=" ";
}
result+=temp;
return result;
}
}
class Address
{
private String streetAddress, city, state;
private long zipCode;
// Constructor: Sets up this address with the specified data
public Address (String street, String town, String st, long zip)
{
streetAddress = street;
city = town;
state = st;
zipCode = zip;
}
// Returns a desciption of this address object
public String toString()
{
String result;
result = streetAddress + " ";
result += city + ", " + state + " " + zipCode;
return result;
}
}
public class StudentBody {
// Creates some Address and Student objects and prints them
public static void main (String[] args)
{
Address school = new Address("800 Lancaster Ave." , "Villanova","PA" , 19085);
Address jHome = new Address("21 Jump Street", "Lynchburg","VA", 24551);
Scanner sc = new Scanner(System.in);
System.out.print("Enter how many Students ? ");
int size = sc.nextInt();
Student array[] = new Student[size];
int i = 0, j = 0;
while(i < array.length)
{
System.out.println();
System.out.println("Enter "+(i+1)+" Student details");
System.out.print("Enter Student First name : ");
String fname = sc.next();
System.out.print("Enter Student Last name : ");
String lname = sc.next();
// adding up to 6 courses to student using random
int random = 1 + (int)(Math.random() * 6);
Course carray[] = new Course[random];
System.out.println();
System.out.println("Enter "+random+" courses");
while(j < carray.length)
{
System.out.println();
System.out.print("Enter Course Team name : ");
String team = sc.next();
System.out.print("Enter course Number : ");
String courseNo = sc.next();
System.out.print("Enter Location name : ");
String location = sc.next();
System.out.print("Enter instructor First name : ");
String insFname = sc.next();
System.out.print("Enter instructor Last name : ");
String insLname = sc.next();
String instructorName = insLname+","+insFname;
Course c = new Course(team,courseNo,location,instructorName);
carray[j] = c;
j++;
}
Student s = new Student(fname,lname,jHome,school,carray);
array[i] = s;
System.out.println();
System.out.println("Student Details : "+s);
i++;
j = 0;
}
System.out.println("Total Number of Students : "+array.length);
System.out.println("Total Number of Courses : "+Course.count);
System.out.println("INDEX"+" "+"Student Name");
int index = 0;
for(Student student : array)
{
System.out.println(index+" "+student.firstName+""+student.lastName);
index++;
}
}
}
V
Explanation / Answer
Hi, this code is working fine.
Save whole code with file name StudentBody.java.
Please let me know in case you get any error.
Do not forgot to mention ERROR (what exactly you will get)
package frstweek;
import java.util.Scanner;
class Course
{
static int count;
String team,courseNumber,location,instructor;
public Course(String team, String courseNumber, String location, String instructor) {
this.team = team;
this.courseNumber = courseNumber;
this.location = location;
setInstructor(instructor);
count++;
}
public String getCourseNumber() {
return courseNumber;
}
public void setCourseNumber(String courseNumber) {
this.courseNumber = courseNumber;
}
public String getInstructor() {
return instructor;
}
public void setInstructor(String instructor) {
this.instructor = instructor;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
public String toString()
{
return team+" "+courseNumber+" "+location+" "+instructor;
}
}
class Student
{
public String firstName, lastName;
private Address homeAddress, schoolAddress;
private Course[] shedule;
// Constructor: Sets up this student with the specified values
public Student (String first, String last, Address home, Address school,Course[] shedule)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
this.shedule = shedule;
}
// Returs a string description of this student object
public String toString()
{
String result;
result = firstName + " " + lastName + " ";
result+= "Home Address: " + homeAddress + " ";
result += "School Address: " + schoolAddress +" ";
result+="Course Details : ";
String temp = "";
for(Course c : shedule)
{
temp+="Team : "+c.team+" Course Number : "+c.courseNumber+" Location : "+c.location+" Instructor Name :"+c.instructor;
temp+=" ";
}
result+=temp;
return result;
}
}
class Address
{
private String streetAddress, city, state;
private long zipCode;
// Constructor: Sets up this address with the specified data
public Address (String street, String town, String st, long zip)
{
streetAddress = street;
city = town;
state = st;
zipCode = zip;
}
// Returns a desciption of this address object
public String toString()
{
String result;
result = streetAddress + " ";
result += city + ", " + state + " " + zipCode;
return result;
}
}
public class StudentBody {
// Creates some Address and Student objects and prints them
public static void main (String[] args)
{
Address school = new Address("800 Lancaster Ave." , "Villanova","PA" , 19085);
Address jHome = new Address("21 Jump Street", "Lynchburg","VA", 24551);
Scanner sc = new Scanner(System.in);
System.out.print("Enter how many Students ? ");
int size = sc.nextInt();
Student array[] = new Student[size];
int i = 0, j = 0;
while(i < array.length)
{
System.out.println();
System.out.println("Enter "+(i+1)+" Student details");
System.out.print("Enter Student First name : ");
String fname = sc.next();
System.out.print("Enter Student Last name : ");
String lname = sc.next();
// adding up to 6 courses to student using random
int random = 1 + (int)(Math.random() * 6);
Course carray[] = new Course[random];
System.out.println();
System.out.println("Enter "+random+" courses");
while(j < carray.length)
{
System.out.println();
System.out.print("Enter Course Team name : ");
String team = sc.next();
System.out.print("Enter course Number : ");
String courseNo = sc.next();
System.out.print("Enter Location name : ");
String location = sc.next();
System.out.print("Enter instructor First name : ");
String insFname = sc.next();
System.out.print("Enter instructor Last name : ");
String insLname = sc.next();
String instructorName = insLname+","+insFname;
Course c = new Course(team,courseNo,location,instructorName);
carray[j] = c;
j++;
}
Student s = new Student(fname,lname,jHome,school,carray);
array[i] = s;
System.out.println();
System.out.println("Student Details : "+s);
i++;
j = 0;
}
System.out.println("Total Number of Students : "+array.length);
System.out.println("Total Number of Courses : "+Course.count);
System.out.println("INDEX"+" "+"Student Name");
int index = 0;
for(Student student : array)
{
System.out.println(index+" "+student.firstName+""+student.lastName);
index++;
}
}
}
/*
Output:
Enter how many Students ? 2
Enter 1 Student details
Enter Student First name : Pravesh
Enter Student Last name : Kumar
Enter 4 courses
Enter Course Team name : CSE
Enter course Number : Computer
Enter Location name : Jamune
Enter instructor First name : Munir
Enter instructor Last name : Hussain
Enter Course Team name : ECE
Enter course Number : Networking
Enter Location name : Daltonganj
Enter instructor First name : Dipu
Enter instructor Last name : raj
Enter Course Team name : EEE
Enter course Number : PowerGrid
Enter Location name : Sikkim
Enter instructor First name : Alex
Enter instructor Last name : Bob
Enter Course Team name : ME
Enter course Number : Graphics
Enter Location name : Ravangla
Enter instructor First name : Sudhir
Enter instructor Last name : Kumar
Student Details : Pravesh Kumar
Home Address:
21 Jump Street
Lynchburg, VA 24551
School Address:
800 Lancaster Ave.
Villanova, PA 19085
Course Details :
Team : CSE Course Number : Computer Location : Jamune Instructor Name :Hussain,Munir
Team : ECE Course Number : Networking Location : Daltonganj Instructor Name :raj,Dipu
Team : EEE Course Number : PowerGrid Location : Sikkim Instructor Name :Bob,Alex
Team : ME Course Number : Graphics Location : Ravangla Instructor Name :Kumar,Sudhir
Enter 2 Student details
Enter Student First name :
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.