Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assessment, you will design and code a simple Java application that defi

ID: 3907522 • Letter: I

Question

In this assessment, you will design and code a simple Java application that defines a class, instantiate the class into a number of objects, and prints out the attributes of these objects in a specific way. You can use either the Toolwire environment or your local Java development environment to complete this assignment.


The requirements of this application are as follows: The application is to define a Java class called Course. The Course class has the following attributes:

1. code - a string field to store the course code (e.g. IT1006)
2. creditHours – an int field to store the credit hours of the course (e.g. 6)
The application then instantiates, in order, seven instances (objects) from the Course class and assigns to each instance the following values:

Course Object creditHours attribute

Also, the application prints out, in the same order as the instantiation order, the attributes of these instances using this format:
[order of instantiation] Course Code (Course Credit Hours)
Successful completion of this assignment will show the correct order of object instantiation and the correct attributes of each object when the application is run. Your program output should look like this sample output:

*FINAL IMPORTANT ITEMS*: The Course class must not contain hard-coded courses. The Course class is meant to be a template for any Course objects the need to be created by the application (created in main()).

The code in main() should use the Course class to create the set of courses specified. It would be a good idea to use an array of Course objects, but this isn't required.

Order of Instantiation Course Object Code attribute

Course Object creditHours attribute

1 IT1006 6 2 IT4782 3 3 IT4789 3 4 IT4079 6 5 IT2230 3 6 IT3345 3 7 IT2249 6

Explanation / Answer

PROGRAM

// Create Course class
class Course
{
String code; // declare string variable code
int creditHours; // declare integer variable creditHours

Course(String code,int creditHours) // create parameter constructor
{
this.code=code;
this.creditHours=creditHours;
}

}

// create main class TestCourse
class TestCourse
{
public static void main(String args[])
{
// declare array of course Objects of Course class
Course course[]=new Course[]{
new Course("IT1006",6),
new Course("IT4782",3),
new Course("IT4789",3),
new Course("IT4079",6),
new Course("IT2230",3),
new Course("IT3345",3),
new Course("IT2249",6)};

System.out.print("--------------------------------------------------------------------- ");

// Display Header
System.out.print("|Order of | Course Object Code | Course Object CreditHours| |Instantiation | attribute | attribute | ");
System.out.print("--------------------------------------------------------------------- ");
// create for loop until 7 data elements
for(int i=0;i<7;i++){
System.out.print("|"+(i+1)+" | "+course[i].code+" | "+course[i].creditHours+" | "); // display Object course with members
System.out.print("--------------------------------------------------------------------- ");

}


}
}

OUTPUT:


F:>javac TestCourse.java

F:>java TestCourse
---------------------------------------------------------------------
|Order of | Course Object Code | Course Object CreditHours|
|Instantiation | attribute | attribute |
---------------------------------------------------------------------
|1 | IT1006 | 6 |
---------------------------------------------------------------------
|2 | IT4782 | 3 |
---------------------------------------------------------------------
|3 | IT4789 | 3 |
---------------------------------------------------------------------
|4 | IT4079 | 6 |
---------------------------------------------------------------------
|5 | IT2230 | 3 |
---------------------------------------------------------------------
|6 | IT3345 | 3 |
---------------------------------------------------------------------
|7 | IT2249 | 6 |
---------------------------------------------------------------------

UPDATED PROGRAM USING ENCAPSULATION

import java.util.Scanner;

// Create Course class
class Course
{
private String code; // declare string variable code
private int creditHours; // declare integer variable creditHours

// implement setter method
public void set(String code,int creditHours)
{
this.code=code;
this.creditHours=creditHours;
}

// implement getter method
public String getCode(){ return this.code;}
public int getCredit(){ return this.creditHours;}
}

// create main class TestCourse
class TestCourse1
{
public static void main(String args[])
{

String c; // declare String variable c
int h; // declare integer variable h
Course course[]=new Course[7]; // Declare array of Objects course
Scanner scr=new Scanner(System.in);
for(int i=0;i<7;i++)
{
course[i]=new Course(); // initialize objects
System.out.print("Enter Code: "); c=scr.next();
System.out.print("Enter Credit Hours: "); h=scr.nextInt();
course[i].set(c,h);

}
System.out.print("--------------------------------------------------------------------- ");

// Display Header
System.out.print("|Order of | Course Object Code | Course Object CreditHours| |Instantiation | attribute | attribute | ");
System.out.print("--------------------------------------------------------------------- ");
// create for loop until 7 data elements
for(int i=0;i<7;i++){
System.out.print("|"+(i+1)+" | "+course[i].getCode()+" | "+course[i].getCredit()+" | "); // display Object course with members
System.out.print("--------------------------------------------------------------------- ");

}


}
}