I\'ve been stimied by this Java program for awhile now. Hoping someone can take
ID: 659867 • Letter: I
Question
I've been stimied by this Java program for awhile now. Hoping someone can take a look at it and help me with the errors. I know that it's been posted many times, but most of the answer's given have arrays in them which haven't been taught yet.
Here's the class:
/*******************************************************************************************
*This class models a cat including name, age, weight, breed, and whether or not they have been declawed.
******************************************************************************************/
import java.util.Scanner;
public class Cat
{
//Class variables
private String name;
private int age;
private double weight;
private String breed;
private boolean is_declawed;
//******************************************************************************************
//constructor
public Cat(){
}
public void setName(String name){
this.name = name;
}
public void setAge(int Age){
this.age = age;
}
public void setWeight(double weight){
this.weight = weight;
}
public void setBreed(String breed){
this.breed = breed;
}
public void setDeclawed(Boolean declawed){
this.is_declawed = is_declawed;
}
//******************************************************************************************
//accessor method to enter Cat names
public String getName()
{
return this.name;
}//end get name
//accessor method to enter Cat ages
public int getAge()
{
return this.age = age;
}//end get age
//accessor method to enter Cat current weights
public double getWeight()
{
return this.weight;
}//end get weight
//accessor method to enter Cat breed
public String getBreed()
{
return this.breed;
}//end get breed
//boolean method to enter Cat claw status
public boolean getIs_declawed()
{
if (this.is_declawed == true)
{
return this.is_declawed;
}
else
{
return false;
}
}//end get claw status
//******************************************************************************************
}
Here's the driver:
/*******************************************************************************************
*This is a driver for the Cat class.
*******************************************************************************************/
import java.util.Scanner;
public class Unit6
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//Input Cats name
System.out.println("Enter cats name:");
String name = input.next();
//Input Cats age
System.out.println("Enter cats age :");
int age = input.nextInt();
//Input Cats weight
System.out.println("Enter cats weight :");
double weight = input.nextDouble();
//Input Cats breed
System.out.println("Enter cats breed :");
String breed = input.next();
//Input value true or false for Cats claw status
System.out.println("Is your cat declawed? (True or False) :");
boolean is_declawed = input.nextBoolean();
//method used to set variables name, age, weight, breed, and claw status for each of 3 Cats
Cat myCat1 = new Cat();
myCat1.setName(name, age, weight);
myCat1.setBreed(breed);
myCat1.set(boolean);
Cat myCat2 = new Cat();
myCat2.set(name, age, weight);
myCat2.set(breed);
myCat2.set(boolean);
Cat myCat3 = new Cat();
myCat3.set(name, age, weight);
myCat3.set(breed);
myCat3.set(boolean);
//provides desired output
System.out.println("The Cats over 3 years old with claws are: ");
if(myCat().getAge() >= 3 && myCat().getIs_declawed== false)
System.out.println("Name: " + getName);
System.out.println("Age: " + getAge + "Years Old:");
}
}
Can anyone see where my errors are? The class compiles fine, but the driver has multiple errors.
Thank you!
Explanation / Answer
public class Cat {
// Class variables
private String name;
private int age;
private double weight;
private String breed;
private boolean is_declawed;
// ******************************************************************************************
// constructor
public Cat() {
}
public void setName(String name) {
this.name = name;
}
public void setAge(int Age) {
this.age = age;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void setBreed(String breed) {
this.breed = breed;
}
public void setDeclawed(Boolean declawed) {
this.is_declawed = is_declawed;
}
// ******************************************************************************************
// accessor method to enter Cat names
public String getName() {
return this.name;
}// end get name
// accessor method to enter Cat ages
public int getAge() {
return this.age = age;
}// end get age
// accessor method to enter Cat current weights
public double getWeight() {
return this.weight;
}// end get weight
// accessor method to enter Cat breed
public String getBreed() {
return this.breed;
}// end get breed
// boolean method to enter Cat claw status
public boolean getIs_declawed() {
if (this.is_declawed == true) {
return this.is_declawed;
} else {
return false;
}
}// end get claw status
// ******************************************************************************************
}
======
import java.util.Scanner;
public class Unit6
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//Input Cats name
System.out.println("Enter cats name:");
String name = input.next();
//Input Cats age
System.out.println("Enter cats age :");
int age = input.nextInt();
//Input Cats weight
System.out.println("Enter cats weight :");
double weight = input.nextDouble();
//Input Cats breed
System.out.println("Enter cats breed :");
String breed = input.next();
//Input value true or false for Cats claw status
System.out.println("Is your cat declawed? (True or False) :");
boolean is_declawed = input.nextBoolean();
//method used to set variables name, age, weight, breed, and claw status for each of 3 Cats
Cat myCat1 = new Cat();
myCat1.setName(name);
myCat1.setBreed(breed);
myCat1.setAge(age);
myCat1.setWeight(weight);
myCat1.setDeclawed(is_declawed);
System.out.println("The Cats over 3 years old with claws are: ");
if(myCat1.getAge() >= 3 && myCat1.getIs_declawed()== false)
System.out.println("Name: " + myCat1.getName());
System.out.println("Age: " + myCat1.getAge() + " Years Old:");
System.out.println("Enter cats name:");
name = input.next();
//Input Cats age
System.out.println("Enter cats age :");
age = input.nextInt();
//Input Cats weight
System.out.println("Enter cats weight :");
weight = input.nextDouble();
//Input Cats breed
System.out.println("Enter cats breed :");
breed = input.next();
//Input value true or false for Cats claw status
System.out.println("Is your cat declawed? (True or False) :");
is_declawed = input.nextBoolean();
//method used to set variables name, age, weight, breed, and claw status for each of 3 Cats
Cat myCat2 = new Cat();
myCat2.setName(name);
myCat2.setBreed(breed);
myCat2.setAge(age);
myCat2.setWeight(weight);
myCat2.setDeclawed(is_declawed);
System.out.println("The Cats over 3 years old with claws are: ");
if(myCat2.getAge() >= 3 && myCat2.getIs_declawed()== false)
System.out.println("Name: " + myCat2.getName());
System.out.println("Age: " + myCat2.getAge() + "Years Old:");
System.out.println("Enter cats name:");
name = input.next();
//Input Cats age
System.out.println("Enter cats age :");
age = input.nextInt();
//Input Cats weight
System.out.println("Enter cats weight :");
weight = input.nextDouble();
//Input Cats breed
System.out.println("Enter cats breed :");
breed = input.next();
//Input value true or false for Cats claw status
System.out.println("Is your cat declawed? (True or False) :");
is_declawed = input.nextBoolean();
//method used to set variables name, age, weight, breed, and claw status for each of 3 Cats
Cat myCat3 = new Cat();
myCat3.setName(name);
myCat3.setBreed(breed);
myCat3.setAge(age);
myCat3.setWeight(weight);
myCat3.setDeclawed(is_declawed);
//provides desired output
System.out.println("The Cats over 3 years old with claws are: ");
if(myCat3.getAge() >= 3 && myCat3.getIs_declawed()== false)
System.out.println("Name: " + myCat3.getName());
System.out.println("Age: " + myCat3.getAge() + "Years Old:");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.