Skip to content

Latest commit

 

History

History
69 lines (57 loc) · 2.85 KB

File metadata and controls

69 lines (57 loc) · 2.85 KB

Java-Assignment-010 - Classes and Objects

Part 1 - Analyze

  • Add Comments to the Code below and label the following:
    1. Class name
    2. All instance variables/fields and their data-types
    3. The Constructor and the Constructor Parameters
    4. Where a Student object gets created.
    5. Where the instance variables value gets set and what its values are.
    6. All the instance methods for the class Student
//Class name: Student
class Student{
    //instance variables:
  //String: name
  //int: rollNo
    private String name;
    private int rollNo;
   //This is the constructor, with parameters of one String and one int
    Student(String s, int r)
    {
   	    name = s;
   	    rollNo = r;
    }
   //This is an instance method for class Student
    void methodForDisplay()
    {
        System.out.println(name+"'s Roll No: "+rollNo);
    }

    public static void main(String[] args) {
        //A Student object has been created here.
      //Additionally, the String name has been set as Rambo
      //The int rollNo has been set at 21
        Student obj1=new Student("Rambo",21);
        obj1.methodForDisplay();
    }
}

Part 2 - Public VS Static

  • Read the W3Schools page on class methods: W3Schools Java Class Methods
  • In your own words, write a few sentences below explaining the difference between static and public methods in relation to a class.

A static method can be called outside of the class. A public method can only be called if an object is created within the method the public method is intended to be called. To properly access this method through the object it should be structured like this: ObjectBeingAccessed.myMethod();

Part 3 - Dogs

  • View the image below, and from the image, construct a Java file Dog that mirrors the diagrammed class and the 3 dog objects. Dog Class

  • Your class should be named Dog

  • You should have private instance variables/fields for all the data members.

  • You should have a constructor that sets the initial state of the data members via passed parameters.

    • Think about what data-types the fields and parameters will need!!!
  • You should have 4 instance methods for eat, run, sleep, and name

  • You should have a main method that creates the 3 Dog objects in the diagram.

  • Make at least 2 of your methods functional (i.e. perform some action)!!!

    • For Example: method eat might take a class parameter named Food (i.e. another class) that has a field weight, and your eat method might reduce the weight in Food.

Part 4 - Turn-In

  • Create a feature branch Feature1
  • Commit your code and push it back to your account.
  • Create a Pull request as you have been doing for all assignments.
  • Paste the Pull request URL back into the Canvass assignment page for credit.