Question

Write a class XXX_Course • A course has a name, a course number, a department code...

Write a class XXX_Course

  • • A course has a name, a course number, a department code (for example, CS, MATH) and a room number.
  • • Include a static variable used to assign the course number to each Course in the constructor. The value of the variable will start at 1000.
  • • Write the instance variables, the accessors (getters), the mutators (setters) and two constructors for the class. One of the constructors should be the no args constructor

Write a second class, XXX_ TestCourse, which will have a main method.

  • • Create an Array of Course objects, which can hold 3 elements.
  • • Using a loop:
    • • Ask the user for the name, department and room number for 3 courses.
    • • Create course objects.
    • • Store the objects in the Array
  • • At the end, traverse the array of courses and print each course on a separate line.

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In case of any query do comment. Please rate answer as well. Thanks

Code:

================ XXX_Course.java===========

public class XXX_Course

{

    private static int cNumber =1000;

   

    //private data members

    private String name;

    private String departmentCode;

    private int roomNumber;

    private int courseNumber;

   

    //default constructor

    public XXX_Course()

    {

        name ="";

        departmentCode ="";

        roomNumber =0;

        courseNumber = cNumber++;

    }

   

    //parameterized constructor

    public XXX_Course(String cname,String dep,int room)

    {

        name = cname;

        departmentCode = dep;

        roomNumber = room;

        courseNumber = cNumber++;

    }

   

    //accessors

    public String getName()

    {

        return name;

    }

   

    public String getDepartmentCode()

    {

        return departmentCode;

    }

   

    public int getRoomNumber()

    {

        return roomNumber;

    }

   

    public int getcNumber()

    {

        return courseNumber;

    }

   

    //mutators

    public void setName(String cname)

    {

        name = cname;

    }

   

    public void setDepartmentCode(String dep)

    {

        departmentCode = dep;

    }

    public void setRoomNumber(int room)

    {

        roomNumber = room;

    }

   

    //string representation of objects

    public String toString()

    {

        return "Course Name: " + name + " Dept: " + departmentCode + " CourseNumber: " + courseNumber + " Room " + roomNumber;

    }

   

}

============ XXX_ TestCourse.java=========

import java.util.*;

public class XXX_ TestCourse

{

                public static void main(String[] args) {

                               

                                XXX_Course[] courses = new XXX_Course[3];

                                String dept,courseName;

                                int room;

                                Scanner scnr = new Scanner(System.in);

                                //using loop take user input to create objects of XXX_Course class and fill the array

                                for (int i =0;i <3 ;i++ )

                                {

                                    System.out.println("Enter Dept");

                                    dept = scnr.nextLine();

                                    System.out.println("Enter room number");

                                    room = Integer.parseInt(scnr.nextLine());

                                    System.out.println("Enter Course name");

                                    courseName = scnr.nextLine();

                                   

                                    courses[i] = new XXX_Course(courseName,dept,room);

                                }

                               

                                System.out.println("Printing the courses");

                                //display the data

                                for (int i =0;i <3 ;i++ )

                                {

                                    System.out.println(courses[i]);

                                }

                               

                }

}

==========screen shot of the code========

Output:

Add a comment
Know the answer?
Add Answer to:
Write a class XXX_Course • A course has a name, a course number, a department code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA Write a class Store which includes the attributes: store name, city. Write another class...

    IN JAVA Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...

  • Write a class Store which includes the attributes: store name, city. Write another class encapsulating an...

    Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method returning the...

  • The object class should be a Student with the following attributes: id: integer first name: String...

    The object class should be a Student with the following attributes: id: integer first name: String last name: String write the accessors, mutators, constructor, and toString(). In your main test class you will write your main method and do the following things: Create an array of Student objects with at least 5 students in your array. Write a sort method that must be your original work and included in the main class. The sort method will sort based on student...

  • [JAVA] Program: Design a Ship class that the following members: A field for the name of...

    [JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...

  • Design a Ship class that has the following class members: A field for the name of the ship...

    Design a Ship class that has the following class members: A field for the name of the ship A field for the year the ship was built A constructor Appropriate accessors and mutators toString method that displays the ships name and the year it was built Design a CruiseShip class the inherits from the Ship class. Include the following members: A field for the max number of passengers A constructor Appropriate accessors and mutators toString method that overrides the Ship toString method. The method...

  • Write a class that encapsulates the concept of a baseball player with the important attributes (name,...

    Write a class that encapsulates the concept of a baseball player with the important attributes (name, position, hits, at bats, batting average). The class will have the following methods: Two constructors (a default and a constructor with all parameters) Accessors, mutators, toString, and equals methods The batting average property will not have a public setter, it will be updated any time the hits and/or at bats is set. The getter will round to three digits (.315 or .276, etc.) All...

  • College of Winston and Charlotte This program will calculate the amount for a semester bill at...

    College of Winston and Charlotte This program will calculate the amount for a semester bill at The College of Winston and Charlotte. Part 1: Create a class Course.java: The class has non-static instance variables: private String department private int courseNumber private int courseCredits private double courseCost The class must have a default constructor which will set values as follows: department = “unknown” courseNumber = 0 courseCost = 0 courseCredits = 0 The class must have a non-default constructor which will...

  • create a Person class with two fields (name(String), age(int)) create all the required methods such as...

    create a Person class with two fields (name(String), age(int)) create all the required methods such as the accessors, mutators, toString(),constructors etc. Use the comparable interface and implement the compareTo method such that the person objects can be compared and sorted according to their age.

  • "Write a class that represents a course at Juniata. A course has a department (ex. CS,...

    "Write a class that represents a course at Juniata. A course has a department (ex. CS, IT, PY, BI), a number (ex. 110, 240, 356), a name (ex. Computer Science 1), and a number of credits. Write a test driver just to make sure your class works. I recommend just instantiating a few courses and displaying them using the __str__ method. If you want to go a little farther, store a few courses in a list and loop through them...

  • Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the...

    Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the type int, assessedPrice of the type double. A constructor for this class should have three arguments for initializing these fields. Add accessors and mutators for all fields except a mutator for yearBuilt, and the method toString. Create a class StreetHouse which extends House and has additional fields: streetNumber of the type int, homeowner of the type String, and two neighbors: leftNeighbor and rightNeighbor, both...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT