help needed . would someone guide me ?? how i can solve the following question . / Create three objects of Cat type Create three objects of type Cat in the method main and fill them with data. Use the class Cat of the first task. Do not create the class Cat. / this is the given code .

public static void main(String[] args) { //add your code here }

public static class Cat {

    public static int count = 0;
    private String name;
    private int age;
    private int weight;
    private int strength;

    public Cat(String name, int age, int weight, int strength) {
        count++;

        this.name = name;
        this.age = age;
        this.weight = weight;
        this.strength = strength;
    }
}

asked 02 Apr '15, 09:56

Muhammad%20Hassan%20Durrani's gravatar image

Muhammad Has...
4311224
accept rate: 0%


link

answered 02 Apr '15, 11:12

Andrey's gravatar image

Andrey
1323
accept rate: 35%

i have to create three objects of cat in main method . what does it mean . which three objects and how ??

(03 Apr '15, 19:02) Muhammad Has...

i created these three objects in main method . running fine but has not pass testing .

     int x = 10;
     int y = 6;
     int z = 30;
(03 Apr '15, 19:03) Muhammad Has...

It's not objects, it's variables. Objects look like this:

Object object = new Object();
(03 Apr '15, 20:04) Andrey

I know it's a quite tricky for the first sight, but it's as simple as defining String or any other type of a data. Imagine that you have a class Person which have some random methods like sleep() and eat(), also it has a constructor with name and age parameters. Now we can create an infinite quantity of instances of this class in other different classes and call any method or field through an instance (object) of class Person.

Person john = new Person("John Smith", 34);
Person clara = new Person("Clara Oswald", 24);
john.sleep();
clara.sleep();
clara.eat();
// etc.
(03 Apr '15, 20:04) Andrey

You should read about classes and objects, it is foundations. Click here.

(03 Apr '15, 20:05) Andrey

got it and done . i was also thinking to creat objects like you mentioned . but when i google it i found the variable declration . then i thought that may be i can create objects like this . thanks mate :)

(08 Apr '15, 18:16) Muhammad Has...
showing 5 of 6 show 1 more comments

Thanx Andrey,, your opinions are realy helpfull.

link

answered 21 Apr '15, 19:50

Zain%20Mehar's gravatar image

Zain Mehar
13
accept rate: 0%

Something like

        import java.util.ArrayList;
        import java.util.List;

        List <Cat> myCats = new ArrayList<Cat>();
        myCats.add(new Cat("Blacky",2,10, 5));
        myCats.add(new Cat("Red",3,12, 2));
        myCats.add(new Cat("Fluffy",5,20, 1));
        for (Cat cat : myCats)
            System.out.println(cat.name + ": age=" + cat.age+" weight="+cat.weight+" strength="+cat.strength);

Blacky: age=2 weight=10 strength=5 Red: age=3 weight=12 strength=2 Fluffy: age=5 weight=20 strength=1

Process finished with exit code 0

link

answered 13 Dec '17, 07:36

Eugen's gravatar image

Eugen
11
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×67
×30
×24

Asked: 02 Apr '15, 09:56

Seen: 1,668 times

Last updated: 13 Dec '17, 07:36