Defining Classes Objectives Understand how to define classes Establish private a
ID: 3791570 • Letter: D
Question
Defining Classes
Objectives
Understand how to define classes
Establish private and public accessible attributes/variables
Instantiating classes into objects
Background
Remember that classes are defined to hold data and behaviors (functions/methods). Sometimes these should be public (and visible to users of the classes) while other times these should be private (accessible only within the class itself).
Directions
Begin an empty console application
Create a new class "Course" that represents data about a college course.
This class should have,
a string to hold a short description of the course
an integer "course number"
an integer "credit hours”
a string "prefix" (such as CSE or CGDD)
Remember to use appropriate scope; your attributes should be private and then provide public get/set accessors for each field.
Create a constructor that takes two strings and two integers (representing values for each attribute)
a. Initialize each variable within the class using these parameters to the constructor
5. Create a constructor that takes in no parameters
a. Initialize each variable within the class to defaults of your choosing
Override the ToString() method to output all field names and values of any instance.
I n m a i n , c reate two instances of the course class; one should use the default constructor
(with no parameters) and one should use the 4-parameter constructor.
Print each course to the screen to show them to the user.
Im having trouble code my main two instances, this is what i have so far
using System;
namespace HaileLab3
{
class MainClass
{
public static void Main (string[] args)
Course c1 = new Course();
Console.WriteLine (c1.ToString());
}
}
using System;
namespace HaileLab3
{
public class Course
{
private string description;
private int coursenumber;
private int credithours;
private string prefix;
//properties
public string Description
{
get { return description; }
set { description = value; }
}
public string Prefix
{
get { return prefix; }
set { prefix = value; }
}
public int Coursenumber
{
get { return coursenumber; }
set { coursenumber = value; }
}
public int Credithours
{
get { return credithours; }
set { credithours = value; }
}
//constructor
public Course ()
{
credithours = 4;
coursenumber = 1101;
prefix = "EGD";
description = "Engineeringgraphics";
}
//overload
public Course (string course, string prefix, int coursenumber, int credithours)
{
this.description = course;
this.prefix = prefix;
this.coursenumber = coursenumber;
this.credithours = credithours;
}
//overriding tostring by parent (object)
public override string ToString ()
{
return ("Description: " + description + " Coursenumber: " + coursenumber + " Credithours: " + credithours + " Prefix: " + Prefix + " ");
}
}
}
Explanation / Answer
Course.java:
public class Course {
private String description;
private int courseNumber;
private int creditHours;
private String prefix;
//parameterized constructor
public Course(String description, int courseNumber, int creditHours,
String prefix) {
this.description = description;
this.courseNumber = courseNumber;
this.creditHours = creditHours;
this.prefix = prefix;
}
//default constructor
public Course() {
creditHours = 4;
courseNumber = 1101;
prefix = "EGD";
description = "Engineeringgraphics";
}
//Getters and Setters
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getCourseNumber() {
return courseNumber;
}
public void setCourseNumber(int courseNumber) {
this.courseNumber = courseNumber;
}
public int getCreditHours() {
return creditHours;
}
public void setCreditHours(int creditHours) {
this.creditHours = creditHours;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
//toString method overriden
@Override
public String toString() {
return "Course [description=" + description + ", courseNumber="
+ courseNumber + ", creditHours=" + creditHours + ", prefix="
+ prefix + "]";
}
public static void main(String[] args) {
Course c1=new Course();
//Creating instance of Couse class using default constructor
Course c2=new Course("Computer Graphics and Designs",5,1102,"CGD");
//Creating instance of Course class using 4-parameter constructor
System.out.println(c1);
System.out.println(c2);
}
}
Output:
Course [description=Engineeringgraphics, courseNumber=1101, creditHours=4, prefix=EGD]
Course [description=Computer Graphics and Designs, courseNumber=5, creditHours=1102, prefix=CGD]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.