+ 4

Using method

I cannot give name to dog. Pls help me name my dog! class Dog { int age; String name; public Dog(String dogName){ name = dogName; } public Dog(int dogsAge) { age = dogsAge; } public void bark() { System.out.println("Woof!"); } public void run(int feet) { System.out.println("Your dog ran " + feet + " feet!"); } public int getAge() { return age; } public String dogName() { return name; } public static void main(String[] args) { Dog spike = new Dog(5); Dog spike = new Dog("Spike"); spike.bark(); spike.name(); spike.run(40); int spikeAge = spike.getAge(); String spikeName = spike.dogName(); System.out.println("Your dog " + dogName + " is " + dogsAge + " years old"); } }

5th Jun 2017, 12:28 PM
Jasurbek Kalandarov
Jasurbek Kalandarov - avatar
6 Answers
+ 1
@James Compilation error
5th Jun 2017, 2:25 PM
Jasurbek Kalandarov
Jasurbek Kalandarov - avatar
0
the above program shows no output
5th Jun 2017, 1:26 PM
Disha
0
the comment is intended to James
5th Jun 2017, 1:26 PM
Disha
0
public class Dog { private int age; private String name; public Dog(int age,String name) { this.age = age; this.name = name; } public String setDogName(String name) { this.name = name; return name; } public int getDogName() { return age; } public int setDogAge(int age) { this.age = age; return age; } public int getDogAge() { return age; } public int computeDogAge() { this.age = age*7; return age; } public String toString() { String dogsname= "Your dog "; String dogsage= "is "; return dogsname + name + "\t" + dogsage + age; } public static void main (String[] args) { Dog dog1 = new Dog(1,"Spike"); Dog dog2 = new Dog(2,"Jed"); Dog dog3 = new Dog(5,"Butch"); System.out.println(dog1); } }
5th Jun 2017, 4:33 PM
Jasurbek Kalandarov
Jasurbek Kalandarov - avatar
0
but still could not add "years old" after dog's age
5th Jun 2017, 4:33 PM
Jasurbek Kalandarov
Jasurbek Kalandarov - avatar
0
Here I removed some code but the result remains same: https://code.sololearn.com/cQcvg1ZSs52E/#java
5th Jun 2017, 5:57 PM
Jasurbek Kalandarov
Jasurbek Kalandarov - avatar