Please help with OOP programming.?
I have to write a program where you can enter 3 cats and info regarding those cats and then out put what was entered using oop programming. I cant get it to output the entries for cat 2 and cat 3. public class Cat private String name// name of cat private int age// age of cat in years private double weight// weight of cat in pounds private String breed// breed of cat private boolean declawed// does cat have claws or not // // This method assigns the name variable public void setNameString name this.name name // end setName // // This method allows name to be entered public String getName return name // end get name // // This method sets breed to newBreed public void setBreedString newBreed breed newBreed // end setBreed // // This method allows breed to be entered public String getBreed return breed // end get Breed // // This method assigns variables for name age and weight public void setString name int age double weight // end name age weight // // public void getAge // end age // // This method sets boolean to false for declawed cats public void setDeclawed this.declawed false // end deClawed // // public boolean getDeclawed return false // end get declawed // // This method prints the Cats name and age public void display System.out.printfName name Breed breed Declawed declawedn this.name this.breed this.declawed // end display import java.util. public class CatDriver public static void main String args Scanner input new ScannerSystem.in Cat cat1 new Cat Cat cat2 new Cat Cat cat3 new Cat // // The following statement gather information about Cat 1 System.out.printEnter the name of Cat 1: String name input.next System.out.printEnter the age of Cat 1: int age input.nextInt System.out.printEnter the weight of Cat 1: double weight input.nextDouble System.out.printEnter the breed of Cat 1: String breed input.next System.out.printDoes the cat 1 have claws? True or False? boolean declawed input.nextBoolean System.out.println // end Cat 1 information inputs // // The following statement gather information about Cat 2 System.out.printEnter the name of Cat 2: input.next System.out.printEnter the age of Cat 2: input.nextInt System.out.printEnter the weight of Cat 2: input.nextDouble System.out.printEnter the breed of Cat 2: input.next System.out.printDoes the cat 2 have claws? True or False? input.nextBoolean System.out.println // end Cat 2 information inputs // // The following statement gather information about Cat 3 System.out.printEnter the name of Cat 3: input.next System.out.printEnter the age of Cat 3: input.nextInt System.out.printEnter the weight of Cat 3: input.nextDouble System.out.printEnter the breed of Cat 3: input.next System.out.printDoes the cat 3 have claws? True or False? input.nextBoolean System.out.println cat1.setNamename cat1.setBreedbreed cat2.setNamename cat2.setBreedbreed cat3.setNamename cat3.setBreedbreed // end Cat 3 information inputs // System.out.printlncat1.getName System.out.printlncat1.getBreed System.out.printlncat2.getName System.out.printlncat2.getBreed System.out. Thanks so for for the help. I do understand that I am not setting the variables correct for cats 2 and 3. However when I do it the way youve stated I get errors. System.out.printEnter the name of Cat 2: cat2.setNameinput.next System.out.printEnter the age of Cat 2: cat2.setAgeinput.next System.out.printEnter the weight of Cat 2: cat2.setWeightinput.next System.out.printEnter the breed of Cat 2: cat2.setBreedinput.next System.out.printDoes the cat 2 have claws? True or False? cat2.setDeclawedinput.next System.out.println 24: cannot find symbol symbol : method setAgejava.lang.String location: class Cat cat1.setAgeinput.next 27: cannot find symbol symbol : method setWeightjava.lang.String location: class Cat cat1.setWeightinput.next
Asked by MBLA about Programming Design
Okay this just outlines what not to do with OOP. All you have here is a simple structure with methods that just return each aspect. You could write this whole mess in a simple imperative language like C in a couple dozen lines and it would run twenty times faster. Even in large scale programs it is rarely beneficial to have this kind of nonsense. You could write a whole frickin operating system in the space you have used to access simple numbers about cats.
Answered by Nick J
Youre only assigning the variables for the first cat. You need to have separate variables for the second and third cat or set the values on each cat after you get the values instead of doing them all at the end.
Answered by Chad
The problem is that you dont do anything with the inputs for cats 2 and 3 and you set them to the same name and breed as cat 1. For instance this: System.out.printEnter the name of Cat 3: input.next gets the input for cat 3s name but doesnt put it anywhere. Here: cat1.setNamename cat1.setBreedbreed cat2.setNamename cat2.setBreedbreed cat3.setNamename cat3.setBreedbreed You set all three cats data using the same variables so they all have identical values. To fix it store the inputs for cats 2 and 3 as you did with cat 1 and call the setName and setBreed methods right after to finish getting inputs. Or you could do things better still like: System.out.printEnter the name of Cat 1: cat1.setNameinput.next
Answered by femtorgon2
No comments:
Post a Comment