0% found this document useful (0 votes)
22 views13 pages

23ID01CE020 (Practical - 4)

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)
22 views13 pages

23ID01CE020 (Practical - 4)

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/ 13

PRACTICAL - 4

1. Classes and Objects


Code:-
class Book {
String title;
String author;
int price;

Book(String book_title, String book_author,


int book_price) {
title = book_title;
author = book_author;
price = book_price;
}

void display() {
System.out.println("title: " + title);
System.out.println("author: " + author);
System.out.println("price: " + price + "\n");
}

void update(String new_title, String


new_author, int new_price) {
title = new_title;
author = new_author;
price = new_price;
}

public class Main {


public static void main(String[] args) {
Book book1 = new Book("The Wonderland",
"Puru", 149);

book1.display();

Name :- PURU JANGID


Enrollment no. :- 23ID01CE020
PRACTICAL - 4
book1.update("The Adventures of Tintin",
"Herge", 151);

book1.display();

}
}
Output:-

2. Access Modifiers

Code:-

class Account {
private int accountNumber;
private String accountHolderName;
private int balance;
private String password;

Account(int accountNumber, String accountHolderName, int balance,


String password) {
this.accountNumber = accountNumber;
this.accountHolderName = accountHolderName;
this.balance = balance;
this.password = password;
}

void display(String password) {


if (this.password == password) {
System.out.println("accountNumber: " + this.accountNumber);

Name :- PURU JANGID


Enrollment no. :- 23ID01CE020
PRACTICAL - 4
System.out.println("accountHolderName: " +
this.accountHolderName);
System.out.println("balance: " + this.balance + "\n");
} else {
System.out.println("invalid password");
}
}

void depositing(int amount, String password) {


if (this.password == password) {
if (amount < 0) {
System.out.println("invalid amount!");
} else {
balance += amount;
}
} else {
System.out.println("invalid pasword");
}
}

void withdrawing(int amount, String password) {


if (this.password == password) {
if (balance < 0) {
System.out.println("insufficient balance!");
} else {
balance -= amount;
}
} else {
System.out.println("invalid pasword");
}
}

public class Main {


public static void main(String[] args) {
Account account1 = new Account(62987351, "Swapnil", 429989,
"Exotic_Swap");

account1.display("Exotic_Swap");

account1.depositing(25000, "Exotic_Swap");

account1.display("Herge");

account1.withdrawing(50000, "Exotic_Swap");

account1.display("Exotic_Swap");

}
}

Name :- PURU JANGID


Enrollment no. :- 23ID01CE020
PRACTICAL - 4
Output:-

3. Constructors

Code:-
class Student {
String name;
int age;
String course;

Student() {
name = "unknown";
age = 0;
course = "unknown";
}

Student(String name) {
this.name = name;
age = 0;
course = "unknown";
}

Student(String name, int age, String


course) {
this.name = name;
this.age = age;

Name :- PURU JANGID


Enrollment no. :- 23ID01CE020
PRACTICAL - 4
this.course = course;
}

void display() {
System.out.println("name: " + name);
System.out.println("age: " + age);
System.out.println("course: " + course +
"\n");
}
}

public class Main {


public static void main(String[] args) {
// Create an array of students
Student[] students = new Student[3];
students[0] = new Student();
students[1] = new Student("Alice");
students[2] = new Student("Bob", 20,
"Computer Science");

for (Student student : students) {


student.display();
}
}
}
Output:-

Name :- PURU JANGID


Enrollment no. :- 23ID01CE020
PRACTICAL - 4
4. The Garbage Collector
Code:-
class Resource {
Resource() {
System.out.println("Resource created");
}

protected void finalize() {


System.out.println("Resource destroyed");
}
}

public class Main {


public static void main(String[] args) {
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();

resource1 = null;
resource2 = null;
resource3 = null;

System.gc();
}
}
Output:-

5. ‘this’ Keyword
Code:-
class Rectangle {

Name :- PURU JANGID


Enrollment no. :- 23ID01CE020
PRACTICAL - 4
int length;
int width;

void setDimensions(int length, int width) {


this.length = length;
this.width = width;
}

int calculateArea() {
return length * width;
}

void compareArea(Rectangle rectangle) {


if (this.calculateArea() >
rectangle.calculateArea()) {
System.out.println("this rectangle has a larger
area");
} else if (this.calculateArea() <
rectangle.calculateArea()) {
System.out.println("the other rectangle has a
larger area");
} else {
System.out.println("the areas are equal");
}
}
}

public class Main {


public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle();
Rectangle rectangle2 = new Rectangle();

rectangle1.setDimensions(5, 10);
rectangle2.setDimensions(10, 20);

System.out.println("rectangle1 area: " +


rectangle1.calculateArea());

Name :- PURU JANGID


Enrollment no. :- 23ID01CE020
PRACTICAL - 4
System.out.println("rectangle2 area: " +
rectangle2.calculateArea());

rectangle1.compareArea(rectangle2);
}
}
Output:-

6. Class vs. Instance Members


Code:-
class Counter {
static int count = 0;
int id;

Counter() {
count++;
id = count;
}

static void resetCounter() {


count = 0;
}

void display() {
System.out.println("ID: " + id + " Count: " +
count);
}
}

public class main {


public static void main(String[] args) {
Counter c1 = new Counter();

Name :- PURU JANGID


Enrollment no. :- 23ID01CE020
PRACTICAL - 4
Counter c2 = new Counter();

c1.display();
c2.display();

Counter.resetCounter();

Counter c3 = new Counter();


Counter c4 = new Counter();
Counter c5 = new Counter();

c3.display();
c4.display();
c5.display();

}
}
Output:-

7. ‘static’ Keyword
Code:-
class MathUtils {
static double PI;
static {
PI = 3.14159;
}

static int add(int a, int b) {


return a + b;
}

Name :- PURU JANGID


Enrollment no. :- 23ID01CE020
PRACTICAL - 4
static int subtract(int a, int b) {
return a - b;
}

static int multiply(int a, int b) {


return a * b;
}

static int divide(int a, int b) {


return a / b;
}

static double circumference(double radius) {


return 2 * PI * radius;
}
}

public class main {


public static void main(String[] args) {
System.out.println("Addition: " +
MathUtils.add(5, 3));
System.out.println("Subtraction: " +
MathUtils.subtract(5, 3));
System.out.println("Multiplication: " +
MathUtils.multiply(5, 3));
System.out.println("Division: " +
MathUtils.divide(5, 3));
System.out.println("Circumference: " +
MathUtils.circumference(5));
}
}
Output:-

Name :- PURU JANGID


Enrollment no. :- 23ID01CE020
PRACTICAL - 4
8. Command Line Arguments
Code:-
public class main8 {
public static void main(String[] args) {
// Check if enough arguments are passed
if (args.length < 4) {
System.out.println("Please enter the
student's name and marks in three subjects.");
return;
}

// Get student's name


String name = args[0];

// Initialize array for marks and parse them


from arguments
int[] marks = new int[3];
for (int i = 0; i < 3; i++) {
marks[i] = Integer.parseInt(args[i + 1]);
}

// Calculate total and average


int total = 0;
for (int i = 0; i < 3; i++) {
total += marks[i];
}
double average = total / 3.0;

// Output student's name and average marks


System.out.printf("Student: %s%n", name);
System.out.printf("Average marks: %.1f%n",
average);

// Find highest and lowest marks


int highest = marks[0];
int lowest = marks[0];
for (int i = 1; i < 3; i++) {

Name :- PURU JANGID


Enrollment no. :- 23ID01CE020
PRACTICAL - 4
if (marks[i] > highest) {
highest = marks[i];
}
if (marks[i] < lowest) {
lowest = marks[i];
}
}

// Output highest and lowest marks


System.out.printf("Highest marks: %d%n",
highest);
System.out.printf("Lowest marks: %d%n",
lowest);
}
}
Output:-

9. Wrapper Classes
Code:-
import java.util.Scanner;

public class main9 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
String input = scanner.nextLine();
scanner.close();

Integer number = Integer.valueOf(input);


Double numberDouble =
Double.valueOf(input);

Name :- PURU JANGID


Enrollment no. :- 23ID01CE020
PRACTICAL - 4
System.out.println("Integer: " + number);
System.out.println("Double: " +
numberDouble);

Integer sum = number + number;


System.out.println("Sum: " + sum);

Integer product = number * number;


System.out.println("Product: " + product);

System.out.println("Is the number even? " +


(number % 2 == 0));
}
}
Output:-

Name :- PURU JANGID


Enrollment no. :- 23ID01CE020

You might also like