Java Programs Record
Java Programs Record
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
COURSE RECORD
Portfolio of Practice
Fourth Semester
Student Name:
Register Number:
Academic Year: 2022 - 2023
Page 1|
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
Certificate
in the course 20CS43P – object oriented programming & design with java
Examiner Signatures:
1. _____________________
2. _____________________
SCHOOL OF MINES KGF, SWETHA.C - LECTURER 2023
Page 2|
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
Table of Contents
10
11
12
13
Page 3|
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
WEEK -1
1. Install and Setup java environment
2. Install java editor (Eclipse for Enterprise Java) and configure workspace
3. Execution of first java program
4. Java code execution process
You can download the JDK 8 from this link click (CTRL + CLICK) here → jdk software
or
You can download from [Link]
For this you have to create an account on Oracle
Step 1: click on the jdk-8u271-windows-x64 exe file and click on Next button
Page 4|
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
Step 2: if you want to change the path change it or else let it be default depends on your
convenience. And click on to Next
Step 3: This gives the window for installing wait till it goes to another window.
Step 4: if you want to change the JRE path you can change it or let it be default depends on
your convenience and click on to Next
Step 5: wait until it completes
Page 5|
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
Step 6: Now you have successfully installed JDK software only when you click Finish/close
button. After installing your JDK your work is not done Still lot more things to do……..
Setting up your path and classpath for making your programs run anywhere in the
drive
Open the command prompt and give javac command you get the above error as javac is not
recognized as an internal or external command. So you need to set the path.
Here how it goes…..
Page 6|
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
Page 7|
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
In this section you have 2 parts one is user variables and system variables
Page 8|
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
Because the jdk is installed in this path and you should take the full path till bin because bin
contains javac tool
Finally click on to Ok
Now open a Fresh command prompt and give javac –version and java -version
Now you can see the version displayed now you path and class path is set successfully and
you can run anywhere in your hard disk drives (c, d, e etc…)
• This [Link] is in the D: drive since you have already set the path and class path.
Any where you can compile and run..
• If you use any of the IDE’s like eclipse IDE, Netbeans IDE you no need to worry of
the path settings. This IDE’s will take care..
• But this path setting should be known by every one because if you sit working in any
of the system in any part of the world unfortunately you might not get this IDE
software at that moment. only option is to use notepad and set the path.
Page 9|
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
{
[Link]("Hello Java");
}
}
Output :
Hello Java
P a g e 10 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
WEEK – 2
[Link], execute and debug programs in java
a. that uses different types of variables and datatypes;
[Link] and resolve issues in the given code snippet
byte b = 100;
short s = 200;
int num = 1331313131;
long l = 516515351;
double num1 = 22.4;
float num2 = 22.4f;
[Link](b);
[Link](s);
[Link](num);
[Link](l);
[Link](num1);
[Link](num2);
}
}
Output:
100
200
1331313131
516515351
P a g e 11 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
22.4
22.4
}
}
Output :
Enter any number :
63
The number entered by user : 63
P a g e 12 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
WEEK - 3
[Link], execute and debug programs in java
a. That uses different types of constructors
b. For expression evaluation
c. To perform autoboxing and unboxing
[Link] and resolve issues in the given code snippet
/* Write a Java Program to define a class, describe its constructor, types of Constructors */
import [Link].*;
class student
{
String name;
int regno;
int marks1, marks2, marks3;
//null constructor
student()
{
name = "raju";
regno = 131234760;
marks1 = 23;
marks2 = 55;
marks3 = 100;
}
// parameterized constructor
student(String n, int r, int m1, int m2, int m3)
{
name = n;
SCHOOL OF MINES KGF, SWETHA.C - LECTURER 2023
P a g e 13 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
regno = r;
marks1 = m1;
marks2 = m2;
marks3 = m3;
}
//copy constructor
student(student s)
{
name = [Link];
regno = [Link];
marks1 = s.marks1;
marks2 = s.marks2;
marks3 = s.marks3;
}
void display()
{
[Link](name+"\t"+regno+"\t"+marks1+"\t"+marks2+"\t"+marks3);
}
}
class studentdemo
{
public static void main(String args[])
{
student s1 = new student();
student s2 = new student("John", 23422, 62,12,45);
student s3 = new student(s2);
P a g e 14 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
[Link]();
[Link]();
[Link]();
}
}
Output :
raju 13123 23 55 100
John 23422 62 12 45
John 23422 62 12 45
3) b) AutoBoxing
Wrapper class Example: Primitive to Wrapper
{
int a = 20;
Integer i = [Link](a);
Integer j = a;
output :
20 20 20
P a g e 15 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
Unboxing
int j = a;
[Link](a+" "+i+" " +j);
}
}
Output :
333
P a g e 16 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
WEEK - 4
[Link] memory monitoring tool and observe how JVM allocates memory
[Link] allocation explanation through the programs
P a g e 17 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
WEEK - 5
[Link], execute and debug programs in java that uses different control
statements.
[Link] Grading system
[Link] and resolve issues in the given code snippet
Wap to accepts average from the user, calculates the grade and prints
import [Link];
public class CalculateStudentGrades{
public static void main(String agrs[]){
Scanner sc = new Scanner([Link]);
[Link]("Enter average of your marks(less than 100)::");
int average = [Link]();
char grade;
if(average>=80){
grade = 'A';
}else if(average>=60&&average<80){
grade = 'B';
}
else if(average>=40&&average<60){
grade = 'C';
}
else{
grade = 'D';
}
switch(grade){
case'A':
P a g e 18 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
[Link]("Excellent!");
break;
case'B':
case'C':
[Link]("well done");
break;
case'D':
[Link]("Your passed");
case'F':
[Link]("Better try again");
break;
default:
[Link]("Invalid grade");
}
[Link]("Your grade i"+grade);
}
}
Output:-
Enter average of your marks(less than 100)::
22
Your passed
Better try again
Your grade D
P a g e 19 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
WEEK - 6
[Link], execute and debug programs in java
a. that uses encapsulation concept.
2. define class & Implement simple calculator and check compliance with
SRP
Encapsulation in Java
File: [Link]
return acc_no;
}
public void setAcc_no(long acc_no)
{
this.acc_no = acc_no;
}
public String getName()
{
return name;
}
public void setName(String name)
P a g e 20 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
{
[Link] = name;
}
{
[Link] = amount;
}
}
File: [Link]
P a g e 21 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
[Link]("mega@[Link]");
[Link](5000);
[Link](acc.getAcc_no()+" "+[Link]()+ " "+[Link]()+"
"+[Link]());
}
}
Output:-
7560 SONU sonu@[Link] 5025.0
class main{
public static void main(String args[]){
char operator;
Double number1,number2,result;
number1 = [Link]();
P a g e 22 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
switch(operator){
case'+':
result = number1+number2;
[Link](number1+"+"+number2+"="+result);
break;
case'-':
case'*':
result = number1* number2;
[Link](number1+"*"+number2+"="+result);
break;
case'/':
result = number1/ number2;
[Link](number1+"/"+number2+"="+result);
break;
default:
[Link]("Invalid operator!");
}
[Link]();
}
}
Output :-
P a g e 23 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
Choose an operator:+,-,*,or /
*
Enter first number : 56
P a g e 24 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
WEEK - 7
[Link], execute and debug programs in java that uses array concept
[Link], execute and debug programs in java to perform string
manipulation
class Testarray{
public static void main(String args[]){
int a[] = new int[5];
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = 40;
a[4] = 50;
for(int i = 0;i<[Link];i++)
[Link](a[i]);
}
}
Output:-
10
20
P a g e 25 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
30
40
50
}
}
}
Output:-
Elements of given array:
1
2
3
P a g e 26 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
WEEK - 8
[Link], execute and debug programs in java that uses inheritance concept
[Link] simple calculator and check compliance with OCP
[Link] file parser and check compliance with SRP and OCP
File: [Link]
class Animal
{
void eat()
{
[Link]("Eating...");
}
}
class Dog extends Animal{
void bark()
{
[Link]("barking..");
}
}
class Dog extends Animal{
void bark()
{
[Link]("barking..");
}
}
Output:-
barking..
Eating...
P a g e 27 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
b) Open/Closed Principle
Double width;
}
public class Run{
SCHOOL OF MINES KGF, SWETHA.C - LECTURER 2023
P a g e 28 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
[Link] = 20.0;
AreaCalculator a = new AreaCalculator();
[Link]([Link](r));
}
}
Output:-
210.0
3.142857142857143
P a g e 29 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
WEEK - 9
[Link], execute and debug programs in java that uses
a. static binding
b. dynamic binding
Static binding
a) Wap for static binding
class Dog
{
private void eat ( )
{
[Link]("dog is eating...");
}
P a g e 30 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
Dynamic binding
Wap for dynamic binding
class Animal
{
void eat( )
{
[Link]("animal is eating...");
}
}
class Dog extends Animal{
void eat(){[Link]("dog is eating...");}
Output :-
dog is eating...
P a g e 31 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
WEEK - 10
1. Code, execute and debug programs in java that uses
2. abstract class to achieve abstraction
[Link] to achieve abstraction
[Link] whether the given code snippet is correct according to abstraction
or not
Wap to execute Interface
{
void print();
}
[Link]("Hello");
}
public static void main(String args[])
{
A obj = new A();
[Link]();
}
}
Output:-
Hello
P a g e 32 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
(divide)
class Honda4 extends Bike
{
void run(){[Link]("running safely");
}
public static void main(String args[])
{
Bike obj = new Honda4();
[Link]();
}
}
output:-
running safely
P a g e 33 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
File: [Link]
}
}
(divide)
class TestBank
{
public static void main(String args[])
{
Bank b;
P a g e 34 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
b = new SBI();
[Link]("Rate of interest is: "+[Link]()+"%");
b= new PNB();
Output:-
Rate of interest is: 7%
Rate of Interest is: 8%
P a g e 35 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
WEEK - 11
[Link], execute and debug programs in java to
a. handles checked and unchecked exceptions
b. read the content of the file and write the content to another file
[Link] exception handling in calculator program
import [Link];
[Link]("Hello");
try{
catch(Exception e){
Output:-
Hello
P a g e 36 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
unchecked
inpuArray[0] = 41;
inpuArray[1] = 98;
inpuArray[2] = 43;
inpuArray[3] = 26;
inpuArray[4] = 79;
[Link](inpuArray[6]);
Output:
at [Link]([Link])
P a g e 37 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
P a g e 38 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
WEEK - 12
[Link] an interface & Implement & check compliance with ISP
// Interface
interface Animal
[Link]("Zzz");
P a g e 39 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
class Main {
[Link]();
[Link](); }}
WEEK - 13
1. Code, execute and debug programs in java to connect to JDBC and perform
basic DB operations
Problem Description
How to connect to a database using JDBC?
Assume that database name is testDb and it has table named employee which has 2
records.
Solution
Following example uses getConnection, createStatement & executeQuery methods
to connect to a database & execute queries.
import [Link].*;
try {
Connection con = [Link] (
"jdbc:derby://localhost:1527/testDb","username", "password");
Statement stmt = [Link]();
ResultSet rs = [Link] ("SELECT * FROM employee");
while ([Link]()) {
P a g e 40 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
no_of_rows++;
}
[Link]("There are "+ no_of_rows + " record in the table");
} catch(SQLException e){
[Link]("SQL exception occured" + e);
}
}
}
Copy and paste the following example in [Link], compile and run as
follows −
import [Link].*;
P a g e 41 |
Object
Object Oriented
Oriented Programming
Programming and Using
and Design Design Using Java
Java 4 SEM 20CS43P
C:\>javac [Link]
Result
The above code sample will produce the following result. The result may vary. You
will get ClassNotfound exception if your JDBC driver is not installed properly.
JDBC Class found
There are 2 record in the table
P a g e 42 |