Java Lab Manual
Java Lab Manual
NAME : ……………………………………………………………
ROLL NO : ……………………………………………………………
CLASS : ……………………………………………………………
2021 REGULATIONS
(5th) ODD SEMESTER
DIPLOMA IN COMPUTER NETWORKING
PSG POLYTECHNIC COLLEGE
DEPARTMENT OF COMPUTER NETWORKING
REGISTER NUMBER
to ……………
AIM:
CODE:
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:
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:
1 JDK 1.6
ALGORITHM:
STEP 3:Declare private instance variables' stuID`, `stuName`, and `stuAge` to store student
information.
variables.
STEP 5:In the `main` method, create a new `Scanner` object to read user input.
STEP 7:Prompt the user to enter the student name and read it using `scanner.nextLine()`.
STEP 8:Prompt the user to enter the student age and read it using `scanner.nextInt()`. Assign
STEP 9:Prompt the user to enter the student ID and read it using `scanner.nextInt()`. Assign
CODE:
import java.util.Scanner;
class StudentData {
public StudentData() {}
this.stuID = stuID;
this.stuName = stuName;
this.stuAge = stuAge;
student1.stuName = scanner.nextLine();
student1.stuAge = scanner.nextInt();
student1.stuID = scanner.nextInt();
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:
1 JDK 1.6
ALGORITHM:
STEP 4:Create a setter method `setValue(int value)` to set the value of `value`.
STEP 7:Prompt the user to enter a value and read it using `scanner.nextInt()`. Assign the
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: ".
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..."
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;
RESULTS:
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:
AIM:
To demonstrate the use of extending interfaces.
CODE:
interface Shape {
void draw();
System.out.println("Drawing a circle");
this.color = color;
}
OUTPUTS:
public class Exp5b {
circle.draw();
circle.setColor("Red");
circle.printColor();
RESULT: