Skip to main content

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();

        

        // Getting data for first student

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

        String name1 = scanner.nextLine();

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

        int age1 = scanner.nextInt();

        scanner.nextLine(); // Consume newline character

        

        // Setting data for first student

        s1.getData(name1, age1);

        

        // Getting data for second student

        System.out.println("Enter name for student 2:");

        String name2 = scanner.nextLine();

        System.out.println("Enter age for student 2:");

        int age2 = scanner.nextInt();

        scanner.nextLine(); // Consume newline character

        

        // Setting data for second student

        s2.getData(name2, age2);

        

        // Printing data for both students

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

        s1.printData();

        

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

        s2.printData();

        

        scanner.close();

    }

}


Algorithm:
  1. Define a class Student2 with private instance variables name and age.
  2. Define a method getData in the Student2 class that takes name and age as arguments and sets the instance variables accordingly.
  3. Define a method printData in the Student2 class that prints the name and age of the student.
  4. Define a class STtest.
  5. Inside the STtest class:
  6. Create a Scanner object to read input from the user.
  7. Create two objects s1 and s2 of the Student2 class.
  8. Prompt the user to enter the name and age for the first student.
  9. Read the name and age entered by the user for the first student and set the data using the getData method of s1.
  10. Prompt the user to enter the name and age for the second student.
  11. Read the name and age entered by the user for the second student and set the data using the getData method of s2.
  12. Print the details of both students using the printData method.
  13. Close the Scanner object.
  14. 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     ...

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) {                ...