0% found this document useful (0 votes)
13 views14 pages

Java Practical

This document is a practical file for a Java programming course at Gurugram University, detailing various experiments and coding exercises. It includes tasks such as creating patterns, implementing method and constructor overloading, and demonstrating multithreading and AWT components. Each experiment is accompanied by code examples and aims to enhance understanding of Java programming concepts.

Uploaded by

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

Java Practical

This document is a practical file for a Java programming course at Gurugram University, detailing various experiments and coding exercises. It includes tasks such as creating patterns, implementing method and constructor overloading, and demonstrating multithreading and AWT components. Each experiment is accompanied by code examples and aims to enhance understanding of Java programming concepts.

Uploaded by

xitebo4137
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

GURUGRAM UNIVERSITY, GURUGRAM

(A State govt. University established under Haryana Act 2017)

programming in java
PRACTICAL FILE

SUBMITTED BY:
SUBMITTED TO:
SHIVANSHI DR.
NEETU SINGLA
B. TECH CSE CORE B (ASSISTANT
PROFESSOR IN
(24100690122) CSE
DEPARTMENT)

S.n Experiment Date Teacher sign


o
1 To write java programs that print different
patterns based on user input for number
of rows.
2 To write java program that do the required
computations using looping constructs.
3 To understand and implement method
overloading in Java.
4 To understand and implement constructor
overloading in Java.
5 To understand and implement method
overriding and constructor overriding in
Java using inheritance.
6 To implement a Java program that counts
the number of objects created using the
static keyword.
7 To understand and implement interfaces
in Java to achieve abstraction and multiple
inheritance.
8 To understand and implement interfaces
in java to achieve abstraction and
inheritance.
9 To demonstrate the use of multithreading
in Java using the Thread class and
Runnable interface.
10 To create a simple calculator using AWT
components and event handling.
11 To demonstrate the use of various AWT
layouts: FlowLayout, BorderLayout,
GridLayout and CardLayout.
12 To understand the basic structure and
execution of java Applet.
Index
Practical: -1
Aim: - To write java programs that print different patterns based on user input for
number of rows.
Code:
import java. [Link];
public class AllPatterns {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int n = [Link]();
// 1. Left-Angled Triangle
[Link]("\n--- Left-Angled Triangle ---");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}
// 2. Right-Angled Triangle
[Link]("\n--- Right-Angled Triangle ---");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
[Link](" "); // Double space to match "* "
}
for (int k = 1; k <= i; k++) {
[Link]("* ");
}
[Link]();
}
// 3. Full Pyramid
[Link]("\n--- Full Pyramid ---");
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
[Link](" "); // Single space for centering
}
for (int k = 1; k <= i; k++) {
[Link]("* ");
}
[Link]();
}
// 4. Inverted Left-Angled Triangle
[Link]("\n--- Inverted Left-Angled Triangle ---");
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}
[Link]();
}
}
Output:-

Practical 2
Aim- To write java program that do the required computations using looping
constructs.
Code:
import [Link];
public class LoopComputation {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter a positive integer (n): ");
int n = [Link]();
// 1. Using a FOR loop
int forSum = 0;
for (int i = 1; i <= n; i++) {
forSum += i;
}
// 2. Using a WHILE loop
int whileSum = 0;
int j = 1;
while (j <= n) {
whileSum += j;
j++;
}
// 3. Using a DO-WHILE loop
int doWhileSum = 0;
int k = 1;
if (n > 0) {
do {
doWhileSum += k;
k++;
} while (k <= n);
}
// Output results
[Link]("\n--- Results ---");
[Link]("Sum using FOR loop: " + forSum);
[Link]("Sum using WHILE loop: " + whileSum);
[Link]("Sum using DO-WHILE loop: " + doWhileSum);
[Link]();
}
}

Practical-3
Aim : To understand and implement method overloading in Java.
Code:
public class OverloadDemo {
public int multiply(int a, int b) {
return a * b;
}
public int multiply(int a, int b, int c) {
return a * b * c;
}
public double multiply(double a, double b) {
return a * b;
}
public static void main(String[] args) {
OverloadDemo demo = new OverloadDemo();
[Link]("Product of 2 ints: " + [Link](5, 4);
[Link]("Product of 3 ints: " + [Link](5, 4, 2));
[Link]("Product of 2 doubles: " + [Link](5.5, 2.0));
}
Practical -4
Aim:- To understand and implement constructor overloading in Java .
Code:
public class Student {
String name;
int age;
public Student(String n) {
name = n;
age = 0; // Default age if not provided
}
public Student(String n, int a) {
name = n;
age = a;
}
public void display() {
[Link]("Student Name: " + name + ", Age: " + age);
}
public static void main(String[] args) {
Student s1 = new Student("Shivanshi");
Student s2 = new Student("Sarthak", 21);
[Link]();
[Link]();
}
}
Output :
Practical-5
Aim: To understand and implement method overriding and constructor
overriding in Java using inheritance.
Code:
class Parent {
String name;
Parent(String name) {
[Link] = name;
[Link]("Parent Constructor: " + name);
}

void showRole() {
[Link]("I am the Parent.");
}
}
class Child extends Parent {
String hobby;
Child(String name, String hobby) {
super(name);
[Link] = hobby;
[Link]("Child Constructor: Hobby is " + hobby);
}
@Override
void showRole() {
[Link]("I am the Child. My name is " + name + " and I like " +
hobby);
}
}
public class OverridingTest {
public static void main(String[] args) {
Child myObj = new Child("Alex", "Coding");
[Link]();
}
}

You might also like