
<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>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages2 Page
-
File Size-