23ID01CE020 (Java Pracrical 3)
23ID01CE020 (Java Pracrical 3)
(1) Create practical examples demonstrating the usage of operators in Java expressions
and statements.
char operator;
operator = sc.next().charAt(0);
number1 = sc.nextInt();
number2 = sc.nextInt();
switch (operator){
case '+':{
break;
case '-':{
break;
case '*':{
case '/':{
break;
default:{
System.out.println("Invalid operator!");
break;
Output :-
(2) Create a simple class named Person with fields for name, age, and gender. Include a
method displayInfo to print out the details of the person.
this.name = name;
this.age = age;
person.displayInfo();
Output :-
(3) Create a class Book with fields title, author, and price. Include a constructor to
initialize these fields and a method to display book details.
this.price = price;
book.displayBookDetails();
Output :-
(4) Create a student class with student_id, subject_code and marks, which takes input
using method getdata() and display result using putdata() method.
int studentId;
String subjectCode;
int marks;
studentId = scanner.nextInt();
subjectCode = scanner.next();
marks = scanner.nextInt();
System.out.println("");
student.getData();
student.putData();
Output :-
String name;
System.out.println("Ea ng...");
System.out.println("Sleeping...");
System.out.println("Barking...");
myDog.name = "Rex";
myDog.eat();
myDog.sleep();
myDog.bark();
Output :-
(6) Create a class Biodata having fields name, qualification and date of birth. Class
Biodata inherits a class Address having fields city and pin. Write a program to display
all details for 2 persons.
class Address {
String city;
int pin;
city = scanner.next();
pin = scanner.nextInt();
String name;
name = scanner.next();
qualifica on = scanner.next();
dateOfBirth = scanner.next();
getAddress();
displayAddress();
person2.getBiodata();
person1.displayBiodata();
person2.displayBiodata();
Output :-