Introduction
In this blog post, we will explore a Java program that demonstrates how to store and display data for students and markers. The program takes input for multiple students and markers, stores their respective attributes, and then prints out the stored data. Let’s analyze the code step by step.
Student Class
package Student; public class Student { public int age; public String name; public int id; }
This class represents a student with attributes such as age, name, and id. It’s a simple data-holding class.
Marker Class
package Marker; public class Marker { public String color; public boolean type; public int inkLevel; }
Similarly, this class represents a marker with attributes for color, type, and ink level.
Client Class
package Client; import Marker.*; import Student.*; import java.util.Scanner; public class Client { public static void main(String[] args) { Scanner s = new Scanner(System.in); int y; int z; System.out.println("How many students data do you want to store:"); y = s.nextInt(); Student x[] = new Student[y]; // Input for students for (int i = 0; i < y; i++) { x[i] = new Student(); System.out.println("Enter your age:"); x[i].age = s.nextInt(); System.out.println("Enter your name:"); x[i].name = s.next(); System.out.println("Enter your Id:"); x[i].id = s.nextInt(); } System.out.println("How many markers data do you want to store:"); z = s.nextInt(); Marker m[] = new Marker[z]; // Input for markers for (int i = 0; i < z; i++) { m[i] = new Marker(); System.out.println("Enter Marker color:"); m[i].color = s.next(); System.out.println("Enter Marker type:"); m[i].type = s.nextBoolean(); System.out.println("Enter Marker Inklevel:"); m[i].inkLevel = s.nextInt(); } System.out.println("\nPrinting Student data"); for (int i = 0; i < y; i++) { System.out.println("ID: " + x[i].id); System.out.println("Name: " + x[i].name); System.out.println("Age: " + x[i].age); } System.out.println("\nPrinting Marker data"); for (int i = 0; i < z; i++) { System.out.println("Type: " + m[i].type); System.out.println("Color: " + m[i].color); System.out.println("Ink Level: " + m[i].inkLevel); } } }
Program Explanation
Importing Necessary Libraries
import Marker.*; import Student.*; import java.util.Scanner;
We import the Marker and Student classes from their respective packages to use them in the Client class.
Main Client Class and Method
public class Client { public static void main(String[] args) { // Program logic goes here } }
This is the main class and method of our program, where the execution begins. Inside this method, we will write the logic for taking user input for students and markers, as well as displaying the stored data.
Input and Storage of Student Data
y = s.nextInt(); Student x[] = new Student[y]; // Input for students for (int i = 0; i < y; i++) { x[i] = new Student(); // Prompting the user for student details and storing them }
We prompt the user to input the number of students they want to store and then proceed to collect the respective student details such as age, name, and id.
Input and Storage of Marker Data
z = s.nextInt(); Marker m[] = new Marker[z]; // Input for markers for (int i = 0; i < z; i++) { m[i] = new Marker(); // Prompting the user for marker details and storing them }
Similar to the student data, we collect the marker details from the user, including color, type, and ink level.
Displaying Student Data
System.out.println("\nPrinting Student data"); for (int i = 0; i < y; i++) { // Displaying student data }
We then display the collected student data to the console.
Displaying Marker Data
System.out.println("\nPrinting Marker data"); for (int i = 0; i < z; i++) { // Displaying marker data }
Finally, we display the collected marker data to the console.
Conclusion
In this blog post, we have explored a Java program that demonstrates how to store and display data for both students and markers. Understanding this basic program is essential for handling and manipulating data in more complex applications in Java. Happy coding!
FAQs
Program for Student and Marker Data
Q1: What is the objective of this program?
The program allows the user to store and display data for students and markers, including details like age, name, ID, marker color, type, and ink level.
Q2: How are classes and objects used in this program?
The program defines classes for Student and Marker, and objects are created to store and manipulate data for multiple students and markers.
Q3: How does the program handle user input?
The program uses the Scanner class to obtain user input for the number of students and markers, as well as their attributes.
Q4: How can I modify the program to add more attributes for students and markers?
You can extend the Student and Marker classes by adding more attributes and updating the input and display logic accordingly.
Q5: Can this program handle a dynamic number of students and markers?
Yes, the program prompts the user for the number of students and markers, making it adaptable to handle varying amounts of data.