#Need help with variable declaration

1 messages · Page 1 of 1 (latest)

errant oreBOT
#

<@&987246399047479336> please have a look, thanks.

errant oreBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

proud latch
#

might want to have 2 fields for the age, one for the year count and one for the month count

uneven blade
proud latch
#

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

uneven blade
#

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();
        }
    }
}
errant oreBOT
# uneven blade ``` public class MainCats { public static void main(String args[]) { ...

Detected code, here are some useful tools:

Formatted code
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();
    }
  }
}
uneven blade
#

The inputCats method can only receive String name and variable age. That's it

proud latch
#

By 0 output you mean nothing is output to the console?

proud latch
#

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

uneven blade
errant oreBOT
# uneven blade ``` public class Cats { private String name; private double ageYears; ...

Detected code, here are some useful tools:

Formatted code
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);
  }
}
uneven blade
#

this is the output. I'm getting 0 for Didi. Didi should be 1 years and 6 months old

proud latch
#

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

uneven blade
#

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);
    }
}

errant oreBOT
# uneven blade ``` public class Cats { private String name; private int ageYears; ...

Detected code, here are some useful tools:

Formatted code
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);
  }
}
uneven blade
#

still getting the same output

#

Dora should be 1 years and 6 months old

proud latch
# uneven blade changed age to int

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

uneven blade
#

changed it to float and still gives me 0 months

proud latch
#

Can I see?

uneven blade
#

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);
}
}

modest canyon
#

ur code is unreadable without syntax highlighting

errant oreBOT
# uneven blade public class Cats { private String name; private double ageYears; ...

Detected code, here are some useful tools:

Formatted code
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);
  }
}
uneven blade
proud latch
#

So you’re turning it into an int

uneven blade
#

WHOAAAAAA

#

URE FREAKING AMAZING MAN!!!

#

THANK YOU KIND SIR!

proud latch
#

🙏🏼