0% found this document useful (0 votes)
25 views9 pages

23ID01CE020 (Java Pracrical 3)

Uploaded by

Puru Jangid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views9 pages

23ID01CE020 (Java Pracrical 3)

Uploaded by

Puru Jangid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PRACTICAL - 3

(1) Create practical examples demonstrating the usage of operators in Java expressions
and statements.

Ans. import java.u l.Scanner;

public class Calculator {

public sta c void main(String[] args){

char operator;

int number1, number2, result;

Scanner sc = new Scanner(System.in);

System.out.println("Choose the operator you want to perform: +, -, *, or /");

operator = sc.next().charAt(0);

System.out.println("Enter first number");

number1 = sc.nextInt();

System.out.println("Enter second number");

number2 = sc.nextInt();

switch (operator){

case '+':{

result = number1 + number2;

System.out.println(number1 + " + " + number2 + " = " + result);

break;

case '-':{

result = number1 - number2;

System.out.println(number1 + " - " + number2 + " = " + result);

break;

case '*':{

result = number1 * number2;

System.out.println(number1 + " * " + number2 + " = " + result);

Name :- PURU JANGID


Enrollment No. :- 23ID01CE020
PRACTICAL - 3
break;

case '/':{

result = number1 / number2;

System.out.println(number1 + " / " + number2 + " = " + result);

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.

Ans. public class Person {

private String name;

private int age;

private String gender;

public Person(String name, int age, String gender) {

this.name = name;

this.age = age;

Name :- PURU JANGID


Enrollment No. :- 23ID01CE020
PRACTICAL - 3
this.gender = gender;

public void displayInfo() {

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

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

System.out.println("Gender: " + gender);

public sta c void main(String[] args) {

Person person = new Person("Puru Jangid", 18, "Male");

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.

Ans. public class Book {

private String tle;

private String author;

private double price;

public Book(String tle, String author, double price) {

this. tle = tle;

Name :- PURU JANGID


Enrollment No. :- 23ID01CE020
PRACTICAL - 3
this.author = author;

this.price = price;

public void displayBookDetails() {

System.out.println("Title: " + tle);

System.out.println("Author: " + author);

System.out.println("Price: $" + price);

public sta c void main(String[] args) {

Book book = new Book("JAVA", "PURU.J", 999);

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.

Ans. import java.u l.Scanner;

public class Student {

int studentId;

String subjectCode;

int marks;

public void getData() {

Scanner scanner = new Scanner(System.in);

Name :- PURU JANGID


Enrollment No. :- 23ID01CE020
PRACTICAL - 3
System.out.print("Enter student ID: ");

studentId = scanner.nextInt();

System.out.print("Enter subject code: ");

subjectCode = scanner.next();

System.out.print("Enter marks: ");

marks = scanner.nextInt();

public void putData() {

System.out.println("");

System.out.println("Student ID: " + studentId);

System.out.println("Subject Code: " + subjectCode);

System.out.println("Marks: " + marks);

public sta c void main(String[] args) {

Student student = new Student();

student.getData();

student.putData();

Output :-

Name :- PURU JANGID


Enrollment No. :- 23ID01CE020
PRACTICAL - 3
(5) Create a simple inheritance program using Java.

Ans. class Animal {

String name;

public void eat() {

System.out.println("Ea ng...");

public void sleep() {

System.out.println("Sleeping...");

class Dog extends Animal {

public void bark() {

System.out.println("Barking...");

public class inheritance {

public sta c void main(String[] args) {

Dog myDog = new Dog();

myDog.name = "Rex";

myDog.eat();

myDog.sleep();

myDog.bark();

Output :-

Name :- PURU JANGID


Enrollment No. :- 23ID01CE020
PRACTICAL - 3

(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.

Ans. import java.u l.Scanner;

class Address {

String city;

int pin;

public void getAddress() {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter city: ");

city = scanner.next();

System.out.print("Enter pin: ");

pin = scanner.nextInt();

public void displayAddress() {

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

System.out.println("Pin: " + pin);

class Biodata extends Address {

String name;

String qualifica on;

Name :- PURU JANGID


Enrollment No. :- 23ID01CE020
PRACTICAL - 3
String dateOfBirth;

public void getBiodata() {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter name: ");

name = scanner.next();

System.out.print("Enter qualifica on: ");

qualifica on = scanner.next();

System.out.print("Enter date of birth: ");

dateOfBirth = scanner.next();

getAddress();

public void displayBiodata() {

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

System.out.println("Qualifica on: " + qualifica on);

System.out.println("Date of Birth: " + dateOfBirth);

displayAddress();

public class DATA {

public sta c void main(String[] args) {

Biodata person1 = new Biodata();

Biodata person2 = new Biodata();

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

Name :- PURU JANGID


Enrollment No. :- 23ID01CE020
PRACTICAL - 3
person1.getBiodata();

System.out.println("Enter details for person 2:");

person2.getBiodata();

System.out.println("\nDetails for person 1:");

person1.displayBiodata();

System.out.println("\nDetails for person 2:");

person2.displayBiodata();

Output :-

Name :- PURU JANGID


Enrollment No. :- 23ID01CE020

You might also like