Design a user defined class called Course. Each course object will hold data abo
ID: 3669019 • Letter: D
Question
Design a user defined class called Course. Each course object will hold data about a course. The data includes courseID, courseName, credits and instructorName. Define the instance variables required of appropriate type. Provide only get methods along with toString( ) and equals( ) methods. Use JavaDoc style comments.
Design another user defined class called Student. Each student object will hold data on studentID, name, GPA, gender, and courseList. Make sure to define studentID of int type, name as String, gender as a char type and courseList as an array of Course. Declare a static variable named nextID initialized to 1000. nextID will be incremented inside the constructor and it’s value would be assigned to studentID. Please note the constructor will not require a separate parameter to initialize studentID. Provide only get methods. Also implement the toString( ) and equals( ) methods. Use JavaDoc style comments.
Create a client program with main method. Start by creating Course objects for each of the courses you are taking this semester. Next create two Student objects. Use the course objects you created to populate the courseLists for the two student objects. Demonstrate the various methods of the Student class.
Explanation / Answer
1.
import java.io.*;
class Course
{
private String courseName;
private String courseInstructor;
public Course( String name, String insname)
{
courseName = name;
courseInstructor = insname;
}
public void setCourseName( String name )
{
courseName = name;
}
public String getCourseName()
{
return courseName;
}
public void setInstructorName( String insname)
{
courseInstructor = insname;
}
public String getInstructorName()
{
return courseInstructor;
}
public void displayMessage()
{
System.out.println( "Course details: "+ getCourseName()+" This course is presented by: "+getInstructorName());
}
}
public class CourseTest
{
public static void main( String [] args )
{
GradeBook gradeBook1 = new Course( "CSC113 Java ProgrammingII!","Alex");
Course1.displayMessage();
String aLine ="";
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println(" Change Instructor's Name to: ");
aLine = input.readLine();
}
catch (Exception e)
{
e.printStackTrace();
}
course1.setInstructorName(aLine);
course1.displayMessage();
}
}
2.
import java.io.*;
class Student
{
int rollno;
String name;
int number_of_subjects;
int mark[];
Student(int roll,String stud_name,int noofsub)throws IOException
{
rollno=roll;
name=stud_name;
number_of_subjects= noofsub;
getMarks(noofsub);
}
public void getMarks(int nosub ) throws IOException
{
mark=new int[nosub];
BufferedReader br= new BufferedReader (new InputStreamReader(System.in));
for (int i=0; i<nosub;i++)
{
System.out.println(“Enter “+i+”Subject Marks.:=> “);
mark[i]=Integer.parseInt(br.readLine());
System.out.println(“”);
}
}
public void calculateMarks()
{
double percentage=0;
String grade;
int tmarks=0;
for (int i=0;i<mark.length;i++)
{
tmarks+=mark[i];
}
percentage=tmarks/number_of_subjects;
System.out.println(“Roll Number :=> “+rollno);
System.out.println(“Name Of Student is :=> “+name);
System.out.println(“Number Of Subject :=> “+number_of_subjects);
System.out.println(“Percentage Is :=> “+percentage);
if (percentage>=70)
System.out.println(“Grade Is First Class With Distinction “);
else if(percentage>=60 && percentage<70)
System.out.println(“Grade Is First Class”);
else if(percentage>=50 && percentage<60)
System.out.println(“Grade Is Second Class”);
else if(percentage>=40 && percentage<50)
System.out.println(“Grade Is Pass Class”);
else
System.out.println(“You Are Fail”);
}
}
class StudentDemo
{
public static void main(String args[])throws IOException
{
int rno,no,nostud;
String name;
BufferedReader br= new BufferedReader (new InputStreamReader(System.in));
System.out.println(“Enter How many Students:=> “);
nostud=Integer.parseInt(br.readLine());
Student s[]=new Student[nostud];
for(int i=0;i<nostud;i++)
{
System.out.println(“Enter Roll Number:=> “);
rno=Integer.parseInt(br.readLine());
System.out.println(“Enter Name:=> “);
name=br.readLine();
System.out.println(“Enter No of Subject:=> “);
no=Integer.parseInt(br.readLine());
s[i]=new Student(rno,name,no);
}
for(int i=0;i<nostud;i++)
{
s[i].calculateMarks();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.