0% found this document useful (0 votes)
35 views

Java Lab Manual

Uploaded by

trw.dcn
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)
35 views

Java Lab Manual

Uploaded by

trw.dcn
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/ 43

PSG POLYTECHNIC COLLEGE

DEPARTMENT OF COMPUTER NETWORKING

NAME : ……………………………………………………………

ROLL NO : ……………………………………………………………

CLASS : ……………………………………………………………

2021 REGULATIONS
(5th) ODD SEMESTER
DIPLOMA IN COMPUTER NETWORKING
PSG POLYTECHNIC COLLEGE
DEPARTMENT OF COMPUTER NETWORKING

JAVA PROGRAMMING LABORATORY


OBSERVATION

REGISTER NUMBER

CERTIFIED BONAFIDE LAB WORK DONE BY

Mr. / Miss. ……………………………….. from the period of …………..

to ……………

DATE: ………… STAFF IN-CHARGE


____________________________________________________________

INTERNAL EXAMINER EXTERNAL EXAMINER


INDEX

EX. NO DATE NAME OF THE EXPERIMENT MARKS SIGNATURE


EX NO: 01-A DISPLAY DEFAULT VALUE OF ALL

DATE: PRIMITIVE DATA TYPES OF JAVA

AIM:

To display default values of all primitive data types of java.

CODE:

public class Exp1a{


static int a;
static float b;
static char c;
static long d;
static double e;
static boolean f;
static byte g;
public static void main (String args[]){
System.out.println("DEFAULT VALUES OF PRIMITIVE DATA TYPE");
System.out.println("INTEGER:"+a);
System.out.println("FLOAT:"+b);
System.out.println("CHARACTER:"+c);
System.out.println("LONG:"+d);
System.out.println("DOUBLE:"+e);
System.out.println("BOOLEAN:"+f);
System.out.println("BYTE:"+g);
}}
OUTPUT:
RESULT:

Thus we have successfully displayed default values of all primitive data types of java.
EX NO: 01-B
PROGRAM USING STRING CLASS AND ITS METHOD
DATE:
AIM:

To program using string class and its method.

CODE:

import java.util.Scanner;
public class Exp1b{
public static void main (String args[]){
System.out.println("STRING METHODS");
System.out.println("Enter the first string A:");
Scanner obj =new Scanner(System.in);
String a=obj.nextLine();
System.out.println("Enter the first string B:");
String b=obj.nextLine();
System.out.println("LENGTH OF STRING A:"+a.length());
System.out.println("LENGTH OF STRING A:"+b.length());
System.out.println("CHARACTER AT INDEX 4 OF STRING A:"+a.charAt(4));
System.out.println("CONCATENATION TWO STRING :"+a.concat(b));
System.out.println("UPPERCASE OF STRING A:"+a.toUpperCase());
System.out.println("LENGTH OF STRING A:"+a.toLowerCase());
System.out.println("STRING A EQUALS STRING B:"+a.equals(b));
System.out.println("STRING A contains the value a:"+a.contains(a));
System.out.println("remove the space of string b:"+b.trim());
System.out.println("STRING A value:"+a.indexOf('h'));
System.out.println("STRING A in bytes:"+a.getBytes());
System.out.println("replace the character in string A:"+a.replace("a","@"));
obj.close();
}
}

OUTPUT:
RESULT:

Thus we have successfully programmed using string class and its method.
EX NO: 02 WRITE A PROGRAM TO GIVE THE EXAMPLE OF THE
CONTROL STATEMENT
DATE: A)IF B)SWITCH C)FOR LOOP D)WHILE E)DO
AIM:
To write a program to give the example of the control statement.
a)if b)switch c)for loop d)while e)do

CODE:
public class Exp2{
public static void main (String args[]){
int x=5;
int y=10;
int num=1;
int sum=2;
int i=0;
System.out.println("if else control statement");
if(x>y){
System.out.println("x is greater than y");
}
else{
System.out.println("y is greater than x");
}
System.out.println("switch control statement");
switch(num){
case 1:
System.out.println("number is 0");
break;
case 2:
System.out.println("number is 1");
break;
}

OUTPUT:
System.out.println("for loop control statement");
System.out.println("the numbers is");
for (int j=1;j<=5;j++){
sum=sum+j;
System.out.println(sum);
}
System.out.println("while loop control statement");
while(i<5){
System.out.println("the even numbers"+i);
i=i+2;
}
System.out.println("do while loop control statement");
int k=1;
do{
System.out.println(k);
k++;
}while(k<=10);
}
}

RESULT:
Thus we have successfully written a program to give the example of the control
statement.a)if b)switch c)for loop d)while e)do.
EXP NO: 03-A
DEFINE A CLASS, DESCRIBE ITS CONSTRUCTOR
DATE: OVERLOAD AND INSTANTIATE ITS OBJECT

AIM:

To define a class, describe its constructor overload and instantiate its object.

COMPONENTS REQUIRED:

S:NO NAME OF THE COMPONENT

1 JDK 1.6

2 A WORKING PC WITH WINDOWS OS

ALGORITHM:

STEP 1:Start the program

STEP 2:Define a class named `StudentData`.

STEP 3:Declare private instance variables' stuID`, `stuName`, and `stuAge` to store student

information.

STEP 4:Create a default constructor `StudentData()` and an overloaded constructor

`StudentData(int stuID, String stuName, int stuAge)` to initialize the instance

variables.

STEP 5:In the `main` method, create a new `Scanner` object to read user input.

STEP 6:Create an instance of `StudentData` named `student1`.

STEP 7:Prompt the user to enter the student name and read it using `scanner.nextLine()`.

Assign the value to `student1.stuName`.

STEP 8:Prompt the user to enter the student age and read it using `scanner.nextInt()`. Assign

the value to `student1.stuAge`.

STEP 9:Prompt the user to enter the student ID and read it using `scanner.nextInt()`. Assign

the value to `student1.stuID`.


OUTPUT:
STEP 10:Print the details of `student1` including ID, name, and age.

STEP 11:End the `main` method.

STEP 12:Stop the program.

CODE:

import java.util.Scanner;

class StudentData {

private int stuID;

private String stuName;

private int stuAge;

public StudentData() {}

public StudentData(int stuID, String stuName, int stuAge) {

this.stuID = stuID;

this.stuName = stuName;

this.stuAge = stuAge;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

StudentData student1 = new StudentData();

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

student1.stuName = scanner.nextLine();

System.out.print("Enter student age: ");

student1.stuAge = scanner.nextInt();

System.out.print("Enter student ID: ");

student1.stuID = scanner.nextInt();

scanner.nextLine(); // Clearing the input buffer


System.out.println("Student 1 Details:");

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

System.out.println("Student Name: " + student1.stuName);

System.out.println("Student Age: " + student1.stuAge);

scanner.close();

RESULT:
Thus we have successfully defined a class, defined its conductor, overloaded the
constructor and instantiated its object.
EXP NO: 03-B DEFINE A CLASS, DEFINE INSTANCE METHODS FOR
SETTING AND RETRIEVING VALUES OF INSTANCE
DATE: VARIABLES AND INSTANTIATE ITS OBJECT.

AIM:
To define a class, define instance methods for setting and retrieving values of instance
variables and instantiate its object.

COMPONENTS REQUIRED:

S:NO NAME OF THE COMPONENT

1 JDK 1.6

2 A WORKING PC WITH WINDOWS OS

ALGORITHM:

STEP 1:Start the program

STEP 2:Define a class named `MyClass`.

STEP 3:Declare a protected instance variable `value` to store an integer value.

STEP 4:Create a setter method `setValue(int value)` to set the value of `value`.

STEP 5:Create a getter method `getValue()` to retrieve the value of `value`.

STEP 6:Create an instance of `MyClass` named `obj`.

STEP 7:Prompt the user to enter a value and read it using `scanner.nextInt()`. Assign the

value to the `userInput` variable.

STEP 8:Call the `setValue` method of `obj` and pass `userInput` as an argument to set the

value.

STEP 9:Print the value of `obj` using the `getValue` method and concatenate it with the string

"Value: ".

STEP 10: End the `main` method.

STEP 11:Stop the program


OUTPUT:
CODE:
import java.util.Scanner;
class MyClass {
protected int value;
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
MyClass obj = new MyClass();
System.out.print("Enter a value: ");
int userInput = scanner.nextInt();
obj.setValue(userInput);
System.out.println("Value: " + obj.getValue());
}
}

RESULT:
Thus we have successfully defined a class, defined instance methods for setting and
retrieved values of instance variables and instantiated its object.
EXP NO: 04-A IMPLEMENT SINGLE INHERITANCE AND DEMONSTRATE
USE OF METHOD OVERRIDING.
DATE:

AIM:
To implement single inheritance and to demonstrate use of method overriding.

CODE:
class student {
public void detalis() {
System.out.println("psg polytechnic college");
}
}
class deparment extends student {
@Override
public void detalis() {
System.out.println("computer networking");
}
}
public class Exp4a{
public static void main(String[] args) {
student student = new student();
student.detalis(); // Output: "Vehicle starting..."

deparment deparment = new deparment ();


deparment .detalis(); // Output: "Car starting..."

student student1 = new deparment();


student1.detalis(); // Output: "Car starting..."
}
}
OUTPUTS:
RESULT:

Thus we have successfully implemented single inheritance and demonstrated the use

of method overriding.
EXP NO: 04-B IMPLEMENT MULTILEVEL INHERITANCE BY APPLYING
VARIOUS ACCESS CONTROLS TO ITS DATA MEMBERS
DATE:

AIM:
To implement multilevel inheritance by applying various access controls to its data
members.

CODE:
class Grandparent {
public int publicVariable = 10;
protected int protectedVariable = 20;
private int a = 30;

public void publicMethod() {


System.out.println("Public method in Grandparent class");
}
protected void protectedMethod() {
System.out.println("Protected method in Grandparent class");
}
private void b() {
System.out.println("Private method in Grandparent class");
}
}

class Parent extends Grandparent {


public void accessVariablesAndMethods() {
System.out.println("Accessing variables and methods in Parent class:");
System.out.println("Public variable: " + publicVariable);
publicMethod();
System.out.println("Protected variable: " + protectedVariable);
protectedMethod();
System.out.println("Private variable (indirect access): " + getPrivateVariable());
b();
OUTPUTS:
}
public int getPrivateVariable() {
return a;
}
}

class Child extends Parent {


public void accessVariablesAndMethods() {
System.out.println("Accessing variables and methods in Child class:");
System.out.println("Public variable: " + publicVariable);
publicMethod();
System.out.println("Protected variable: " + protectedVariable);
protectedMethod();
System.out.println("Private variable (indirect access): " + getPrivateVariable());
b();
}
}

public class Exp4b {


public static void main(String[] args) {
Child child = new Child();
child.accessVariablesAndMethods();
}
}

RESULTS:

Thus we have successfully implemented multilevel inheritance by applying various


access controls to its data members.
EXP NO: 05-A DEMONSTRATE THE USE OF IMPLEMENTING
INTERFACES
DATE:

AIM:
To demonstrate the use of implementing interfaces.

CODE:
import java.util.Scanner;
class Exp5a implements area{
public void calculate(){
int area = lenght*width;
System.out.println("LENGTH:" +lenght);
System.out.println("WIDTH:" +width);
System.out.println("AREA OFN THE BOX:" +area);
}
public static void main(String args[]){
System.out.println("Enter the lenght and width:");
int width,lenght;
Exp5a box=new Exp5a();
box.calculate();
}
}
interface area{
Scanner obj= new Scanner(System.in);
int lenght=obj.nextInt();
int width=obj.nextInt();
void calculate();

}
OUTPUTS:
RESULTS:

Thus we have demonstrated the use of implementing interfaces.


EXP NO: 05-B
DEMONSTRATE THE USE OF EXTENDING INTERFACES
DATE:

AIM:
To demonstrate the use of extending interfaces.

CODE:
interface Shape {

void draw();

interface ColorfulShape extends Shape {

void setColor(String color);

class Circle implements ColorfulShape {

private String color;

public void draw() {

System.out.println("Drawing a circle");

public void setColor(String color) {

this.color = color;

public void printColor() {

System.out.println("The color of the circle is: " + color);

}
OUTPUTS:
public class Exp5b {

public static void main(String[] args) {

Circle circle = new Circle();

circle.draw();

circle.setColor("Red");

circle.printColor();

RESULT:

Thus we have successfully demonstrate the use of extending interfaces.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy