#Need help with variable declaration
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
might want to have 2 fields for the age, one for the year count and one for the month count
Oh yeah i forgot to mention that my instructor wants us to create only one variable for the age
You can store the age as a float representing years. So if an kid is 1.5 years old, that’s 1 year and 6 months
So to get the 1 part (the year part), simply cast the float to an int. It will remove the decimals. So (int) age
For the months, let’s think about it.
One year is 12 months. So if you have the age, what happens when you multiply it by 12?
1 year -> 1*12 = 12
1.5 years -> 1.5*12 = 18
Etc etc. so multiplying the age gives you the amount of months
This isn’t quite what you want yet though
Because 1.5 should be “1 year and 6 months”, not 18 months
For this we can use the modulo operator %
Since 18%12 = 6. 6 months. % is the remainder of the division operation (18/12 is 1 remainder 6
So to isolate the years you can do (int) age. To isolate the months you can multiply by 12 and use the modulo operator. Keep in mind the modulo operator only works on ints, so we will cast 1 more time to get:
(int) (age*12)%12
public class MainCats {
public static void main(String args[])
{
Cats cats[] = new Cats [5];
cats[0] = new Cats("Diego",2);
cats[1] = new Cats("Dora", 1.5f);
cats[2] = new Cats("Didi", 1);
cats[3] = new Cats("Digimon",8);
cats[4] = new Cats("Doraemon",5);
System.out.println("---CATS INFORMATION---");
for(int i = 0; i<5;i++)
{
cats[i].DisplayCats();
}
}
}
Detected code, here are some useful tools:
public class MainCats {
public static void main(String args[] ) {
Cats cats[] = new Cats[5] ;
cats[0] = new Cats("Diego", 2);
cats[1] = new Cats("Dora", 1.5f);
cats[2] = new Cats("Didi", 1);
cats[3] = new Cats("Digimon", 8);
cats[4] = new Cats("Doraemon", 5);
System.out.println("---CATS INFORMATION---");
for (int i = 0; i < 5; i++) {
cats[i] .DisplayCats();
}
}
}
my main class
The inputCats method can only receive String name and variable age. That's it
By 0 output you mean nothing is output to the console?
You’ve created 2 fields in this class for age. If you only need one, only have 1 field for age, then you can either have a separate method to calculate the years/month by getYears() or something, or do that logic in your display function
Also your class should be called Cat as an object of that class is a cat, not multiple cats
Also variables and methods should start with lowercases
public class Cats {
private String name;
private double ageYears;
public void InputCats(String name, double age) {
this.name = name;
this.ageYears = (int)age;
}
public void DisplayCats() {
System.out.println("Cat name : " + name);
System.out.println("Cat age : " + (int)ageYears + "Years "+ (int)(ageYears*12)%12 + "Months");
}
Cats(String name, double age)
{
InputCats( name, age);
}
}
Detected code, here are some useful tools:
public class Cats {
private String name;
private double ageYears;
public void InputCats(String name, double age) {
this .name = name;
this .ageYears = (int ) age;
}
public void DisplayCats() {
System.out.println("Cat name : " + name);
System.out.println("Cat age : " + (int ) ageYears + "Years " + (int ) (ageYears * 12) % 12 + "Months");
}
Cats(String name, double age) {
InputCats(name, age);
}
}
this is my latest code
this is the output. I'm getting 0 for Didi. Didi should be 1 years and 6 months old
You want to store the age as a float or a double. Then, when you want to get the years/months out of it, you can do what we talked about before with casting to get the years and % to get the months
Right now you’re casting go an int right away, stripping away the decimals
public class Cats {
private String name;
private int ageYears;
public void InputCats(String name, double age) {
this.name = name;
this.ageYears = (int)age;
}
public void DisplayCats() {
System.out.println("Cat name : " + name);
System.out.println("Cat age : " + (int)ageYears + "Years "+ (int)(ageYears*12)%12 + " Months");
}
Cats(String name, double age)
{
InputCats( name, age);
}
}
Detected code, here are some useful tools:
public class Cats {
private String name;
private int ageYears;
public void InputCats(String name, double age) {
this .name = name;
this .ageYears = (int ) age;
}
public void DisplayCats() {
System.out.println("Cat name : " + name);
System.out.println("Cat age : " + (int ) ageYears + "Years " + (int ) (ageYears * 12) % 12 + " Months");
}
Cats(String name, double age) {
InputCats(name, age);
}
}
changed age to int
still getting the same output
Dora should be 1 years and 6 months old
Well that’s not going to work, is it? If someone is 1 year and 6 months old, that’s 1.5 years. You can’t have an int with the value of 1.5
So the age of a can’t should be a floating point number, so either a float or a double
changed it to float and still gives me 0 months
Can I see?
public class Cats {
private String name;
private double ageYears;
public void InputCats(String name, double age) {
this.name = name;
this.ageYears = (int)age;
}
public void DisplayCats() {
System.out.println("Cat name : " + name);
System.out.println("Cat age : " + (int)ageYears + "Years "+ (int)(ageYears*12)%12 + " Months\n");
}
Cats(String name, double age)
{
InputCats( name, age);
}
}
ur code is unreadable without syntax highlighting
Detected code, here are some useful tools:
public class Cats {
private String name;
private double ageYears;
public void InputCats(String name, double age) {
this .name = name;
this .ageYears = (int ) age;
}
public void DisplayCats() {
System.out.println("Cat name : " + name);
System.out.println("Cat age : " + (int ) ageYears + "Years " + (int ) (ageYears * 12) % 12 + " Months\n");
}
Cats(String name, double age) {
InputCats(name, age);
}
}
oops sorry
You are doing (int) in your constructor
So you’re turning it into an int
🙏🏼