import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public Cat(String name, Breed breed, double weigth) { this.name = name; this.breed = breed; this.weight = weight; } public Breed getBreed() { return breed; } public double getWeight() { return weight; } //other accessors and mutators ...... } public class Breed { private String name; private double averageWgt; public Breed(String name,double averageWgt) { this.name = name; this.averageWgt= averageWgt; } public double getWeight() { return averageWgt; } //other accessors and mutators ...... } (1).Identify each line as being either valid or Invalid statemnet and TELL WHY its invalid in the follwing main(). public class Q1Main { 1.Breed persian = new Breed("Persian",10.0); _____ 2.Cat chacha = new Cat("Cha Cha",persian,12.0);____ 3.Cat bombom = new Cat("Bom Bom" , "mix", 10.0);_____ 4.Cat puffpuff = new Cat("Puff Puff",chacha,9.0);___________ 5.double diff = chacha.getWeight()-persian.getWeight();_________ 6.System.out.println(puffpuff.getBreed().getWeight());________ } (2).Given the Cat and Breed classes on p.1,what will be the output of the follwing code?????? public class Q2Main { public void main (String[]args) { Cat MyCat = new Cat("winky", new Breed("mix",10.5),9.5); System.out.println(myCat.getweight()); System.out.println(myCat.getBreed().getWeigth()); } }
1.Breed persian = new Breed("Persian",10.0); --->Valid
//_____ 2. Cat chacha = new Cat("Cha Cha",persian,12.0);--->valid
//3. Cat bombom = new Cat("Bom Bom" , "mix", 10.0);--->Invalid because there is no such constructors
//4.Cat puffpuff = new Cat("Puff Puff",chacha,9.0);--->Invalid because there is no such constructors defined
//5.
double diff = chacha.getWeight()-persian.getWeight();---> Valid
//6.
System.out.println(puffpuff.getBreed().getWeight());-->Valid
Output is 0.0 and 10.5
See The Output
See the Image
import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public...
4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...
import java.util.Scanner; public class StudentClient { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("Smith", "123-45-6789", 3.2); Student s3 = new Student("Jones", "987-65-4321", 3.7); System.out.println("The name of student #1 is "); System.out.println("The social security number of student #1 is " + s1.toString()); System.out.println("Student #2 is " + s2); System.out.println("the name of student #3 is " + s3.getName()); System.out.println("The social security number...
public class Animal { private String name; //line 1 private int weight; //line 2 private String getName(){ return name; } //line 3 public int fetchWeight(){ return weight; } //line 4 } public class Dog extends Animal { private String food; //line 5 public void mystery(){ //System.out.println("Name = " + name); //line 6 System.out.println("Food = " + food); //line 7 } } I want to know the super...
GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance variable name of type String private String boatClass; //private instance variable boatClass of type String private int regNum; //private instance variable regNum of type int private Set<String> crew = new HashSet<String>(); public void setName(String name) { this.name = name; } public void setBoastClass(String boatClass) { this.boatClass = boatClass; } public void setRegNum(int regNum) { this.regNum = regNum; } public String getName() { return name;...
Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() { this.numOffspring = 0; } public Person (int numOffspring) { this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) { super(name, birthYear, weight, height, gender, numCarryOn); if(numOffspring < 0) { this.numOffspring = 0; } this.numOffspring = numOffspring; } public int getNumOffspring() { ...
import java.util.Scanner; public class MPGMain { public static void main(String[] args) { Scanner input = new Scanner(System.in); Mileage mileage = new Mileage(); System.out.println("Enter your miles: "); mileage.setMiles(input.nextDouble()); System.out.println("Enter your gallons: "); mileage.setGallons(input.nextDouble()); System.out.printf("MPG : %.2f",mileage.getMPG()); } } public class Mileage { private double miles; private double gallons; public double getMiles()...
public class Item implements Comparable { private String name; private double price; /** * Constructor for objects of class Item * @param theName name of item * @param thePrice price of item */ public Item(String theName, double thePrice) { name = theName; price = thePrice; } /** * gets the name * @return name name of item */ public String getName() { return name; } /** * gets price of item * @return price price of item */...
public class Player { private String name; private int health; public Player(String name) { this.name = name; } } Write a complete method using java to find a Player by name in an array of Player objects. Use a linear search algorithm. The method should either return the first Player object with the requested name, or null if no player with that name is found.
public class Pet { //Declaring instance variables private String name; private String species; private String parent; private String birthday; //Zero argumented constructor public Pet() { } //Parameterized constructor public Pet(String name, String species, String parent, String birthday) { this.name = name; this.species = species; this.parent = parent; this.birthday = birthday; } // getters and setters public String getName() { return name; ...
make this program run import java.util.Scanner; public class PetDemo { public static void main (String [] args) { Pet yourPet = new Pet ("Jane Doe"); System.out.println ("My records on your pet are inaccurate."); System.out.println ("Here is what they currently say:"); yourPet.writeOutput (); Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the correct pet name:"); String correctName = keyboard.nextLine (); yourPet.setName (correctName); System.out.println ("Please enter the correct pet age:"); int correctAge = keyboard.nextInt (); yourPet.setAge (correctAge); System.out.println ("Please enter the...