Can anyone tell me how to use the main and the class together. Do I write them i
ID: 3642347 • Letter: C
Question
Can anyone tell me how to use the main and the class together. Do I write them in different places and how do I do that?For example, this is the class:
public class Dog {
public String name;
public String breed; public int age;
public void writeOutput() {
System.out.println("Name: " + name);
System.out.println("Breed: " + breed);
System.out.println("Age in calendar years: " +age);
System.out.println("Age in human years: " + getAgeInHumanYears());
System.out.println();
} public int getAgeInHumanYears() {
int humanAge = 0;
if (age <= 2) {
}
else
}
}
{ }
humanAge = 22 + ((age-2) * 5); return humanAge;
and this is the main:
public class DogDemo {
public static void main(String[] args) {
}
}
Dog balto = new Dog();
balto.name = "Balto"; balto.age = 8;
balto.breed = "Siberian Husky";
balto.writeOutput();
Dog scooby = new Dog();
scooby.name = "Scooby";
scooby.age = 42;
scooby.breed = "Great Dane";
System.out.println(scooby.name + " is a " +
scooby.breed + ".");
System.out.print("He is " + scooby.age + " years old, or ");
int humanYears = scooby.getAgeInHumanYears();
System.out.println(humanYears + " in human years.");}}
I know that the uses the Dog class, but I have no idea how and where to write the Dog class and make the main use it.
For example I know we call the class Scanner in import java.util.Scanner
but how do I call the class Dog and where do I write it in the first place.
Also I use eclipse.
Explanation / Answer
I went back and looked at some of my old Eclipse Java projects. You should be able to just make sure both your DogDemo class and Dog class files are in the same folder (and same package, if you are using one) and they should be able to talk to each other just fine. With Eclipse I didn't have to do anything special to import the file. Since they will be in the same folder, Eclipse recognizes them as in the same "work" library or something like that.