How to load some values to an instance of an array of objects?

maurirlz

I'm doing some excersises i found online, trying to understand OOP and such, my problem is as it follows, i have 2 classes, mainly a class called Book and one Called Author.

Author posseses 3 attributes: string name, string email and char gender. Book posses 4: String bookname Author[] authors double price int qty

My question is, since i didn't and don't understand what is Author[] supposed to mean, is an Array of objects that stores multiple book authors? if it's that, how should i proceed with it on the main class, should i create an instance of the object Author[] and with a for loop start giving him values? how are the values(name,email,gender) of that object stored in author1[] for example and how should i load values to that object array?

Trying to learn java by myself.

public class Book {
    protected static final int QTY_DEF = 0;
    private String name;
    private Author[] authors;
    private double price;
    private int qty;
        public Book(String name, Author[] authors, double price, int qty) {
        this.name = name;
        this.authors = authors;
        this.price = price;
        this.qty = qty;
    }

    }
public class Author {
    private String name;
    private String email;
    private char gender;

    public Author(String name, String email, char gender) {
        this.name = name;
        this.email = email;
        this.gender = gender;
    }

}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String name;
        String authorname;
        String email;
        int price;
        int qty;
        char gender;


        Author[] author1 = new Author[3];
        System.out.println("Author's name: ");

        System.out.println("Author's email: ");
        email = sc.nextLine();
        System.out.println("Author's gender: ");
        gender = sc.next().charAt(0);
runstache

Author[] represents and array of the Class Author. So in this property on your Book class you would have an array containing Authors with their own name, email and gender.

Now in the Main class there is an array created of Authors with a size of 3. So you can realistically put 3 Authors in that array until it is full and would need to be expanded.

To do that you can do that with a For loop or directly insert them to their positions on the array:

//Set a new Author per position directly
authors[0] = new Author("Jeff Smith", "[email protected]", "M".toCharArray()[0]);
authors[1] = new Author("Jane Doe", "[email protected]", "F".toCharArray()[0]);
authors[2] = new Author("Brian  Smith ", "[email protected]", "M".toCharArray()[0]);

Or with a For Loop where you set the name, email and gender to new values each time through the loop.

String name; 
String email;
char gender;

for (int i = 0; i < authors.length; i++) {
  authors[i] = new Author(name, email, gender);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use .some() for all values in an array with different objects

Access instance variable values in array of objects: Ruby

How to use Array.protoype.map() on array of objects to filter out some specific keys based on it's values?

Filter array of objects by values when not all objects have some keys

How To load the array values as Dynamic?

Angular interpolation lags rendering some values & how to remove one item from the array of typescript objects

How to create an array of classes and instance objects with it in Swift?

how to call destructor on some of the objects in a Dynamic Array

How to filter an array of objects, based on some fields?

How to remove some identical objects in array(js)?

How to merge objects and sum just some values of duplicate objects?

How to write query for condition of some objects must NOT be in array of objects and one of some other objects Must be in array of object?

How to save and load an array list of objects?

swap some values in an array of objects while maintain sorted others

Array of objects with some objects containing another array, need to filter it by values within the object containing an array

How to filter some numerical values from an array?

how to get some values of an array php

how to get some of values from multidimensional array

How to compare values in an array, that is inside an array of objects, with a property of the objects?

How to get an array of the values of all properties of objects inside an array of objects?

How to get values from one array of objects into another array of objects

How to create an array of objects based on values of another array of objects?

How to create a new array using the values of an array of objects within objects?

How to search an array of instance variables inside an array of objects in Java?

How to load values into an array list from a resource

how to load values into extern shared array

How to merge some properties in array of objects into another array?

How to get values from array of objects in javascript

How to loop and push objects values to array?