Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9147431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:59:15+00:00 2026-06-17T10:59:15+00:00

I have to write a class encapsulating a course. Where a course is assumed

  • 0

I have to write a class encapsulating a course. Where a course is assumed to have three attributes: a code name, a description and number of credits. I must include a constructor, the accessors and mutators, and methods toString and equals.

As part of this assignment, I have to write a client class to test all the methods in the Course class. I believe I have the Course class finished but am having trouble writing the client. For everything I try I get the error “Non-Static variable this can not be referenced from a static context”. What am I doing wrong?

Ok, I have rewritten my code. I am still unsure about how to get the mutator methods to work and not sure how to ask the user for input to set all the values for a new course and then output them to the screen while using accessor and mutator methods. Please help me guys. Thanks

public class ManualClass 
{
public static void main(String[] args) 
{
    Course compSci = new Test2().new Course("Comp Sci","IT1101",3.0);
    System.out.println(compSci+ "\n");


    Course dave = new Test2().new Course("Hist1111","World History",4.0);     
    System.out.print(dave.getCourseCode() + " " + dave.getDescription() 
                + " " + dave.getCreditHours() + "\n");
}

public class Course 
{
//Instance Variables
private String courseCode;
private String description;    
private Double creditHours;

public Course() 
{
    courseCode = null;
    creditHours = 0.0;
    description = null;
}

//Constructor
public Course(String courseCode, String description, double creditHours) 
{    
    this.courseCode = courseCode; 
    this.description = description;
    this.creditHours = creditHours;
}

//Accessors (Getters)
public String getCourseCode() 
{
    return courseCode;
}

public String getDescription()
{
    return description;
}

public double getCreditHours()
{
    return creditHours;
}

//Mutators (Setters)
public void setCourseCode(String CourseCode)
{
 this.courseCode = courseCode;
}

public void setDescription(String description)
{
 this.description = description;
}

public void setCreditHours(double creditHours)
{
 this.creditHours = creditHours;
}

    // toString Method
    public String toString()
    {
        DecimalFormat creditsFormat = new DecimalFormat ("#0.0");

        return "Code: " + courseCode + "; Description: " 
               + description + "; creditHours: " 
               + creditsFormat.format(creditHours);
    }

    // Equals Method
    public boolean equals(Object o)
    {
        if (!(o instanceof Course))
            return false;

        else
        {
            Course objCourse = (Course) o;
            if (courseCode.equals(objCourse.courseCode)
                && description == objCourse.description
                && creditHours == objCourse.creditHours)

                return true;

            else
                return false;
        }
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:59:16+00:00Added an answer on June 17, 2026 at 10:59 am

    What you are writing is a classical school boy procedural language code. Not object oriented. Let me explain the reasoning behind this statement.

    When writing oop code, you need to consider “change”. Every course that you create in the future will not have the same attributes.

    For example, I will share what I think your requirements will look like. Every course will undergo a phase wise maturity cycle. Some courses just need to be described enough and you will stop there. Some courses will have books associated with it, and some hyperlinks. Some courses will have images and some will have video files associated with it. Some will have been very mature and they will need complete course management, counselling, enrollment, student entry management, student performance management and finally student certificates and rewards.

    If you think like a student you will create 1 class with all these attributes and you will have null’s for all the fields you dont need.

    If you think like a mature student you will probably create multiple classes like CourseLinksOnly, CourseImagesOnly, CourseImagesAndVideos, CourseVideosOnly.

    If you think like a architect, you will model your class with real world business oriented names and features. CourseBasic, CourseOnline, CourseClassroom, CourseStarted, CourseCompleted, CourseArchived. And each of these will probably inherit from each other. But at the same time the remaining part of your code will not refer to the objects, they will refer to interfaces (contracts) instead. And I can go on…..

    My advice, is to take your learning to a new level very very quickly. Think completely like a architect. A good book to start studying is Head First Design Patterns Java. The first 3 chapters are enough to get your head straight and out of the procedural language way of thinking :).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have write an Query where the table name is student has columns, class,
If I have to write a singleton class in C++ I will be using
I have a class A and write a subclass B. A has only one
I have a class using ReaderWriterLockSlim with a read method and a write method
I have a custom container class for which I'd like to write the iterator
I have a program for a C class I need to write. The program
I will write a SSH communicator class on Python. I have telnet communicator class
I want to write in Delphi (2009 - so I have generic dictionary class)
I have a multi-R/W lock class that keeps the read, write and pending read
I have a problem with portlet. When i write in portlet.xml standart line <portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.