Lecture Notes. Creating a Simple Java Class

Lecture Notes. Creating a Simple Java Class

<p>Lecture Notes. Creating a Simple Java Class</p><p>This class is used to represent a cat. For now the class only stores the cat’s name and weight.</p><p>Cat Class package cis260.matos; public class Cat { // defining a SIMPLE cat class // to be expanded soon...</p><p>// class variables private String name; private double weight;</p><p>// constructor(s) public Cat (String theName, double theWeight) { name = theName; weight = theWeight; }</p><p> public Cat (String theName) { name = theName; weight = 7; }</p><p> public Cat (double theWeight) { name = "N.A."; weight = theWeight; }</p><p> public Cat() { name = "****"; weight = 0; }</p><p>// mutators public String getName() { return (name); }</p><p> public void setName(String theName) { name = theName; }</p><p> public double getWeight() { return (weight); }</p><p> public void setWeight(double theWeight) { weight = theWeight; } // user-defined methods public void showData() { System.out.println("Name:\t\t" + name + "\nWeight:\t\t" + weight); } }</p><p>Driver Class package cis260.matos; public class CatDriver {</p><p>/** * testing a home-made Cat class */ public static void main(String[] args) {</p><p>Cat c1 = new Cat("Fluffy", 20); c1.showData();</p><p>Cat c2 = new Cat(); c2.setName("Leo"); c2.setWeight(10.5); c2.showData();</p><p>Cat c3 = new Cat("Dragon"); c3.showData();</p><p>Cat c4 = new Cat(9.5); c4.showData();</p><p>String myCatName = c1.getName(); System.out.println(myCatName);</p><p>}</p><p>}</p><p>Results</p><p>Name: Fluffy Weight: 20.0 Name: Leo Weight: 10.5 Name: Dragon Weight: 7.0 Name: N.A. Weight: 9.5 Fluffy</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us