Domanda

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;
        }
    }
}
}
È stato utile?

Soluzione 2

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 :).

Altri suggerimenti

As Course is a non-static inner class, you need to create a qualified instance of Course using an instance of Manual_Class:

Course csi = new Manual_Class().new Course("CSI", "Comp Sci", 3.0);

Alternatively, you could move the class declaration of Course out of the the scope of your top level class Manual_Class. In that case, you could use your current syntax.

Also, you can only have one public class declaration per file, which should be your top level class. You can remove that keyword from the Course class declaration.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top