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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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)

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
Transcribed Image Text: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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education