Skip to main content

Write a java program to create a class Student with data name, city and age along with method addData and printData to input and display the data. Create the two objects s1, s2 to declare and access the values.

 CODE:


import java.util.Scanner;


class Student {

    private String name;

    private String city;

    private int age;


    // Method to input data

    public void addData(String name, String city, int age) {

        this.name = name;

        this.city = city;

        this.age = age;

    }


    // Method to display data

    public void printData() {

        System.out.println("Name: " + name);

        System.out.println("City: " + city);

        System.out.println("Age: " + age);

    }

}


public class StudentTest {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        // Creating objects of Student class

        Student s1 = new Student();

        Student s2 = new Student();


        // Input data for student 1

        System.out.println("Enter details for student 1:");

        System.out.print("Name: ");

        String name1 = scanner.nextLine();

        System.out.print("City: ");

        String city1 = scanner.nextLine();

        System.out.print("Age: ");

        int age1 = scanner.nextInt();

        scanner.nextLine(); // Consume newline character


        // Input data for student 2

        System.out.println("\nEnter details for student 2:");

        System.out.print("Name: ");

        String name2 = scanner.nextLine();

        System.out.print("City: ");

        String city2 = scanner.nextLine();

        System.out.print("Age: ");

        int age2 = scanner.nextInt();

        scanner.nextLine(); // Consume newline character


        // Adding data to objects

        s1.addData(name1, city1, age1);

        s2.addData(name2, city2, age2);


        // Printing data of both students

        System.out.println("\nDetails of student 1:");

        s1.printData();

        System.out.println("\nDetails of student 2:");

        s2.printData();


        scanner.close();

    }

}


Algorithm:
  1. Define a class Student with private instance variables name, city, and age.
  2. Define a method addData() in the Student class that takes name, city, and age as arguments and sets the instance variables accordingly.
  3. Define a method printData() in the Student class that prints the name, city, and age of the student.
  4. Define a class StudentTest.
  5. Inside the StudentTest class:
    • Create a Scanner object to read input from the user.
    • Create two objects s1 and s2 of the Student class.
    • Prompt the user to enter details for the first student (name, city, and age).
    • Read the details entered by the user for the first student and set the data using the addData method of s1.
    • Prompt the user to enter details for the second student (name, city, and age).
    • Read the details entered by the user for the second student and set the data using the addData method of s2.
    • Print the details of both students using the printData method.
  6. Close the Scanner object.
  7. End of the program.






Popular posts from this blog

krushnaaa

  import java . awt .* ; import java . awt . event .* ; public class SimpleAWTExample {     public static void main ( String [] args ) {         // Create a Frame         Frame frame = new Frame ( "Simple AWT Example" );         // Create a Button         Button button = new Button ( "Click Me!" );         // Set layout for the Frame         frame . setLayout ( new FlowLayout ());         // Add the Button to the Frame         frame . add ( button );         // Set size of the Frame         frame . setSize ( 300 , 200 ); // Width: 300 pixels, Height: 200 pixels         // Make the Frame visible         frame . setVisible ( true );         // Add a WindowListener to handle closing event     ...

Write a program to create a class Student2 along with two method getData (), printData () to get the value through argument and display the data in printData. Create the two objects s1, s2 to declare and access the values from class STtest.

 CODE: import java.util.Scanner; class Student2 {     private String name;     private int age;          // Method to set data     public void getData(String name, int age) {         this.name = name;         this.age = age;     }          // Method to print data     public void printData() {         System.out.println("Name: " + name);         System.out.println("Age: " + age);     } } public class STtest {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);                  // Creating objects of Student2 class         Student2 s1 = new Student2();         Student2 s2 = new Student2();              ...

Create an in-order threaded binary search tree and perform the traversals.(Double Thread)

 CODE: #include <iostream> using namespace std; struct Node {     int data;     Node* left;     Node* right;     bool leftThread;     bool rightThread; }; Node* createThreadedBST(int arr[], int n) {     Node* root = nullptr;     for (int i = 0; i < n; ++i) {         Node* newNode = new Node;         newNode->data = arr[i];         newNode->left = newNode->right = nullptr;         newNode->leftThread = newNode->rightThread = true;         if (root == nullptr) {             root = newNode;         } else {             Node* current = root;             Node* parent = nullptr;             while (true) {                ...