3. We will construct a public class Dog, with instance variables name (a String)
ID: 3635894 • Letter: 3
Question
3. We will construct a public class Dog, with instance variables name (a String), breed (a String), and age (an int).(5) (a) Declare the instance variables. Use good programming practice.
public class Dog
{
(5) (b) Write a constructor for this class.
(5) (c) Write a mutator method setAge that resets the value of age.
(5) (d) Write a toString method to return a String that will print in the form
Dog...
Name : Ralph
Breed: Beagle
Age : 5
(This is only an example. Your toString method must be general!)
Explanation / Answer
please rate - thanks
import java.util.*;
public class testDog
{
public static void main (String[] args)
{ int age;
String s;
Dog a=new Dog("",0,"");
Scanner in=new Scanner(System.in);
System.out.println ( "Enter Name of Dog: ");
s=in.nextLine();
System.out.println ("Enter the age of the Dog: ");
age=in.nextInt();
a.setName(s);
a.setAge(age);
in.nextLine();
System.out.println ( "Enter breed of Dog: ");
s=in.nextLine();
a.setBreed(s);
System.out.println(a);
}
}
------------------------------
public class Dog
{ private int Age = 0;
private String Name;
private String breed;
public Dog(String name, int age, String b)
{Name=name;
Age=age;
breed=b;
}
public String getBreed()
{return breed;
}
public void setBreed(String b)
{breed=b;
}
public String getName()
{return Name;
}
public void setName(String dogname)
{Name = dogname;
}
public int getAge()
{return Age;
}
public void setAge(int age)
{Age = age;
}
public String toString()
{return "Name: "+Name+" Breed: "+breed+" Age: "+Age;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.