Question 2 In general, an array is a number of

Berikut ini adalah pertanyaan dari affamera pada mata pelajaran Bahasa lain untuk jenjang Sekolah Menengah Atas

Question 2 In general, an array is a number of items arranged in some specified way for storing information on multiple devices. The class Staff definition is a given below: public class Staff { private String name; private String department; private int ID; Staff (String Name, String Department, int id) { this. nane Name; this. department = Department; this. ID id; = = public void display () MM System.out.println (“Staff name:"+ this.name); System.out.println ("department:" + this Department); System.out.println("ID:" + this. ID); } } Figure 1: Staff class VAN Write a Java program named Testing that requires user to input the name, department and ID for 30 staff objects. These objects will be created and kept in an array of staff named Staff[ ]. Once all these objects are created, display the properties of each of these 30 objects through the displayEmployee () method. Use the method from the JOptionPane class to get the input from the user.​

Jawaban dan Penjelasan

Berikut ini adalah pilihan jawaban terbaik dari pertanyaan diatas.

Testing.java (Main class)
import javax.swing.*;
/**
* Testing.java
* This class contains the main method which creates an array of 30 Staff objects and displays the
information for all
* the objects. This class creates an array of Staff, prompts user for information of all the 30 objects, add
them into
* the array, and displays the information for all the objects in the array using a for loop.
* @author YOUR_NAME
* @version 1.0
* @since 10/04/2022
* */ public class Testing {
/**
* This is the main method which is responsible for running the program.
* @param args command line arguments
* */ public static void main(String[] args) {
int n = 30; // variable to store the size of the array
Staff[] staffs = new Staff[n]; // initialize an array for 30 Staff objects
/* use a for loop to prompt user for information for all the 30 Staff objects and add them to the array
*/
for(int i = 0; i < n; i++){ // loop to read information for 30 Staff objects from user using JOptionPane
int ID = Integer.parseInt(JOptionPane.showInputDialog("Staff #" + (i + 1) + ":\n" +
"Enter ID:")); // read Staff ID from user
String name = JOptionPane.showInputDialog("Staff #" + (i + 1) + ":\n" +
"Enter name:"); // read Staff name from user
String department = JOptionPane.showInputDialog("Staff #" + (i + 1) + ":\n" +
"Enter department:"); // read Staff department from user
Staff staff = new Staff(name, department, ID); // create an object of Staff using the data entered by
user
staffs[i] = staff; // add the Staff created to the array
}
/**
* For-each loop to display the information for all the Staff objects in the array using the method
'display' of
* the Staff class.
* */
System.out.println("\nALL STAFFS:\n-----------"); // print a header before printing all the Staffs
for(int i = 0; i < n; i++) { // loop to traverse the array to print the information of all the Staff objects
staffs[i].display(); // call the 'display' method of each Staff object to display their information
System.out.println(); // print an extra newline after displaying every Staff object
}
}
}

Semoga dengan pertanyaan yang sudah terjawab oleh ECAH00 dapat membantu memudahkan mengerjakan soal, tugas dan PR sekolah kalian.

Apabila terdapat kesalahan dalam mengerjakan soal, silahkan koreksi jawaban dengan mengirimkan email ke yomemimo.com melalui halaman Contact

Last Update: Sat, 09 Jul 22