#Classes [Define values for the attributes]

1 messages · Page 1 of 1 (latest)

sonic stag
#

The main class is the class Computer, where i have all the attributes for the others sub-classes. Some of the attributes are defined randomly and i don't know if I'm setting them in the correct part of the Contructor code or not.

public class Computer {
    //Attributes
    private int ID;
    private int RAM;
    private int HDD;
    private double CPU;
    private String level;
    private String architecture;

    //Constructors
    public Computer() {
        String[] possiblesArq = {"x64", "ARM"};
        int arqIndex = (int)(Math.random() * possiblesArq.length);
        this.architecture = possiblesArq[arqIndex];
    }

    public Computer(int ID, int RAM, int HDD, float CPU, String level, String architecture) {
        //this();
        this.ID = ID;
        this.RAM = RAM;
        this.HDD = HDD;
        this.CPU = CPU;
        this.level = level;
        this.architecture = architecture;
    }
class Server extends Computer{
    // Constructors
    public Server() {
        super();
        setLevel("Cloud");

        Random random = new Random();
        setRAM((int)Math.pow(2, (random.nextInt((9 - 7) + 1) + 7)));
        setHDD((int)Math.pow(2, (random.nextInt((14 - 10) + 1) + 10)));
        setCPU((float)random.nextInt((4 - 3) + 1) + 3);
    }

/*    public Server(int ID, int RAM, int HDD, double CPU, String level, String architecture) {
        this();
        this.
    }*/```

Should i define in Server() or in Server(attributes) ?
stark flowerBOT
#

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

snow owl
sonic stag
#

It's suppose to attribute aleatory values

#

I generate the computers in other class

#

in the main

snow owl
sonic stag
#
    private static void initialisePopularise(int numComputers, ArrayList<Computer> computers) {
        int idCounter = 0;
        for (int i = 0; i < numComputers; i++) {
            Server server = new Server();
            server.setID(idCounter);
            idCounter++;
            Laptop laptop = new Laptop();
            laptop.setID(idCounter);
            idCounter++;
            RPI RPI = new RPI();
            RPI.setID(idCounter);
            idCounter++;
            computers.add(server);
            computers.add(laptop);
            computers.add(RPI);
        }
    }```
stark flowerBOT
sonic stag
#

its in the main

snow owl
sonic stag
#

it's better?

snow owl
#

There is no reason for a computer to itiniatise itself randomly

#

And what if you only want certain values random ?

#

what if you want to use your own random object ?

#

what if you want to weight certain values ?
what if you want to have certains values in particular ?

#

You default constructor does something hyper specific

sonic stag
#

so the default constructor should have nothing?

snow owl
#

that should never be useful except in your hyper specific case

#

not only that

#

but that's not what a default constructor should have, it's misleading at best

snow owl
sonic stag
#

you mean the default constructor is the one that dont return something right?

snow owl
#

?

#

You shouldn't have a default constructor

#

it makes no sense

#

you shouldn't allow to create an invalid object

sonic stag
#

probably it doesn't but is required by the university

snow owl
#

...unless it is an assignment and you have too

sonic stag
#

normally i keep empty

snow owl
#

but note that they are teaching bad practices

sonic stag
#

so in this case

#

should i keep the default constructor empty

#

and inicialize the values radomly in the main?

snow owl
#

you should do what your assignment tells you

sonic stag
#

in the initialisePopularise() method?

#

usiging the setters?

snow owl
#

you should do what your assignemtn tells you

sonic stag
#

using

#

man

#

the assigment dont tell me nothing about the format of the class

#

i just know that i have to keep the default constructor

snow owl
#

show your assignment please

sonic stag
#

it's in portuguese

#

but wait

snow owl
#

ah

sonic stag
#

The NCS Laboratory (Networks, Communications, and Security) has commissioned the development of a program to manage its ICT (Information and Communication Technologies) infrastructure. The laboratory has various types of computers, including servers, laptops, and Raspberry Pis. The servers operate at the Cloud level, laptops at the Edge level, and Raspberry Pis at the IoT (Internet of Things) level. In addition to their level within the ICT infrastructure, each computer has an ID generated by the Helpdesk, RAM, storage space (disk), processing capacity (CPU), and an architecture that can be either x64 or ARM. For each type of computer, the initial specifications are randomized based on the following ranges:

#

Note: For RAM and disk values, the result must be a power of 2.

The goal is to develop a program to manage the computers within the NCS Laboratory. The following tasks must be completed:

A. Create the necessary classes to represent the problem as described.

B. Create a class named "NCSLab" and define a structure (an ArrayList of Computers) that contains 5 servers, 5 laptops, and 5 Raspberry Pis to test the functionalities described below.

C. Develop code to print all computers, displaying their ID, RAM, disk, CPU, and level.

D. Develop code to display all computers with x64 architecture.

E. Develop code to calculate the power consumption of each computer in watts, using the following formulas:

#

olutions that do not follow object-oriented programming principles, inheritance, and polymorphism concepts will be penalized. Solutions should adhere to best programming practices. The use of instanceOf(), getClass(), or similar methods is not allowed, and neither static methods (except for main) nor static variables should be used.

snow owl
#

where does it say that you should have a default constructor ?

sonic stag
#

my teacher

snow owl
sonic stag
#

assume that i need the default constructor

snow owl
#

right then add it

#

Solutions should adhere to best programming practices.
But like, they teach you about bad practices and ask you to do them, but also say to use best practices 🤷

#

anyway

#

create this default constructor

#

but don't use it

#

use the other one

#

and well, create a server class, it makes no sense to have it, but I guess they want you to have it

sonic stag
noble viper
#

It's better to separate the responsability, like Alathreon said it shouldn't be in the constructor.

I would create a static function that generates the random values and uses the constructor (that isn't random), then return the new object

sonic stag
#

so that function should be in the main() right?

#

then i use the setters to set the values to the class right?

noble viper
#

like java var sv = Server.newRandom();

sonic stag
#

ok so inside of each sub class i create the method newRandom() and then in the main i attribute the values to the sub class created?

snow owl
sonic stag
#

like this?

#
public class Computer {
    //Attributes
    private int ID;
    private int RAM;
    private int HDD;
    private double CPU;
    private String level;
    private String architecture;

    //Constructors
    public Computer() {
/*        String[] possiblesArq = {"x64", "ARM"};
        int arqIndex = (int)(Math.random() * possiblesArq.length);
        this.architecture = possiblesArq[arqIndex];*/
    }

    public Computer(int ID, int RAM, int HDD, float CPU, String level, String architecture) {
        //this();
        this.ID = ID;
        this.RAM = RAM;
        this.HDD = HDD;
        this.CPU = CPU;
        this.level = level;
        String[] possiblesArq = {"x64", "ARM"};
        int arqIndex = (int)(Math.random() * possiblesArq.length);
        this.architecture = possiblesArq[arqIndex];
    }```
stark flowerBOT
snow owl
#

no

#

don't make the randomness in the constructor

noble viper
#

the point is

#

move the random logic from constructor to a separated function

sonic stag
#

to the methods part of the class?

noble viper
#

For example, I have this class:```java
public class Person {
int id;
String name;

public Person(int id, String name) {
this.id = id;
this.name = name;
}
}The constructor must stay like that, you add the randomness logic in a separated function:java
public class Person {
int id;
String name;

public Person(int id, String name) {
this.id = id;
this.name = name;
}

public static Person newRandom() {
int rdmId; //... random variable
String rdmName; //... random variable
return new Person(rdmId, rdmName);
}
}```

#

and instead of instantiating like java Person p = new Person();You usejava Person p = Person.newRandom();If you want a random object

noble viper
snow owl
noble viper