Sample Output Enter Dept CS Enter room number 204 Enter Course name CPS 2231 Enter Dept CS Enter room number 220 Enter Course name CPS 2232 Enter Dept CS Enter room number 204 Enter Course name CPS 2232 Printing the courses Course Name: CPS 1231 Dept: CS CourseNumber: 1000 Room 204 Course Name: CPS 2231 Dept: CS CourseNumber: 1001 Room 220 Course Name: CPS 2232 Dept: CS CourseNumber: 1002 Room 204
Error in my Java code! I am not getting the intended results, here are my two classes and output.
public class XXX_Course {
// Initialize variables
String Name;
String deptCode;
int roomNo;
private static int courseNm=1000;
int courseid;
// Constructors
XXX_Course() {
Name = "";
deptCode = "";
roomNo = 0;
}
public XXX_Course(String Name, String deptCode, int roomNo) {
this.Name = Name;
this.deptCode = deptCode;
this.roomNo = 0;
}
// Get and set methods.
public void setName (String Name) {
this.Name = Name;
}
public void setdeptCode (String deptCode) {
this.deptCode = deptCode;
}
public void setroomNo (int roomNo) {
this.roomNo=roomNo;
}
String getName() {
return this.Name;
}
String getdeptCode() {
return deptCode;
}
int getroomNo() {
return roomNo;
}
int getcourseNo() {
return courseNm++;
}
// tostring
public String toString()
{
return "Course Name: "+ getName() + " Dept: "+
getdeptCode() + " Course Number: " + getcourseNo() +
" Room "+ this.getroomNo();
}
}
import java.util.*;
public class TestCourse {
public static void main(String[] args) {
// Initializing Scanner
Scanner reader = new Scanner(System.in);
// Array of objects (3)
XXX_Course [] obj = new XXX_Course [3];
for (int i=0; i< 3; i++) {
int rn;
// Asking user
System.out.println("Enter the department:");
String de = reader.nextLine();
System.out.println("Enter the room number:");
rn = reader.nextInt();
System.out.println("Enter the course name:");
String nam = reader.nextLine();
// Using nextline()
reader.nextLine();
// Call to constructor
XXX_Course e = new XXX_Course();
obj[i] = e;
}
for (int i = 0; i < obj.length; i ++){
System.out.println(obj[i]);
} //end of for
}
}
My output:
Course Name: Dept: Course Number: 1000 Room 0
Course Name: Dept: Course Number: 1001 Room 0
Course Name: Dept: Course Number: 1002 Room 0
Intended output:
(refer to photo)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images