UG B.sc. Computer Science 13044-Java Programming-Lab
UG B.sc. Computer Science 13044-Java Programming-Lab
Program :
Output :
Program :
Output:
Display my Address, values from variables
K.Sekar
No : 546 4th Cross Street
Coimbatore 641046
3
Program :
Input :
Profile Preparation Program
Type you details as follows
Type your name :
pals
Type your age :
25
Type your weight :
36.36
Type your gender :
M
Output :
My Profile
-----------
Name : pals
Age : 25
weight : 36.36
gender : m
5
Program :
Input :
Output :
Factorial for 7 is : 5040
6
5. Simple Calculator
Objective : understanding conditional statements and arithmetic expressions
Program :
}
}
if(result!=1)
{
System.out.println(result);
}
Input :
Simple Calculator
---------------------
Enter the operand 1 (value) :
26
Enter operator
+
Enter the operand 1 (value) :
12
Output:
Program :
if(language1 > 39 && language2 > 39 && maths >39 && science>39 && socialscience>39)
System.out.println("Overall Result : PASS" );
else
System.out.println("Overall Result : FAIL" );
System.out.println("------------------------------------------------------------ ");
}
Input :
-----------------------------------
Enter the name :pals
Enter the Language 1 mark : 65
Enter the Language 2 mark : 59
Enter the Mathematics marks : 89
Enter the Science marks : 88
Enter the Social science marks : 93
Output:
------------------SSLC Student Marksheet-------------------
------------------------
Name : pals
Subjects Marks Status
----------------------------------------
Language 1 65 Pass
Language 2 59 Pass
Mathematics 89 Pass
Science 88 Pass
Social Science 88 Pass
Total = 394
average = 78
Overall Result : PASS
--------------------------------------------
10
Output :
The numbers you entered :
56 65 88 99 77 44
The Biggest no is : 99
The smallest no is : 44
11
8. Multiplication table
Objective : understanding the looping construction, arithmetic expression and formatted output
Program :
public class JavaProgramsAll {
public static void main(String[] args) {
// Printing the multiplication table
Output:
Selected Table No :8
and Iteration upto 5
1X8=8
2 X 8 = 16
3 X 8 = 24
4 X 8 = 32
5 X 8 = 40
12
9. Function overloading
Program :
}
13
Input
12 , 18
47 , 53.26
93.75 , 32
159.84, 123.42
Output
Program :
int hsc_total_marks;
float hsc_percentage;
void input_hsc_details()
{
Scanner kinput = new Scanner(System.in);
15
Input
Output :
----------------------------
Your SSLC total marks : 450
Your SSLC percentage is : 90.0
----------------------------
----------------------------
Your HSC total marks : 1127
Your HSC percentage is : 187.33
----------------------------
16
Objective : understanding how to pack the related classes into single unit.
Program :
import mypackages.*;
public class JavaProgramsAll {
public static void main(String[] args) {
//Packages . . .
MyResidensialAddress myResidensialAddress = new MyResidensialAddress();
MyOfficeAddress myOfficeAddress = new MyOfficeAddress();
myResidensialAddress.DisplayAddress();
myOfficeAddress.DisplayAddress();
}
}
Add class file 1 into this package
package mypackages;
package mypackages;
public class MyResidensialAddress {
String myResidentialAddress = "No 163 Pillayar Koil Street, \nCrosscut Road, \nMadurai 641538";
public void DisplayAddress()
{
System.out.println("\n-----------------------------------\n");
System.out.println("\nFrom mypackages Class 1 :\nMy Residential Address is \n" +
myResidentialAddress);
System.out.println("\n-----------------------------------\n");
}
}
17
Output :
-----------------------------------
From mypackages Class 1 :
My Residential Address is
No 163 Pillayar Koil Street,
Crosscut Road,
Madurai 641538
-----------------------------------
From mypackages Class 2 :7
My Office Address is
No 566, Indian Overseas Bank
Airport Campus,,
Chennai 641538
-----------------------------------
18
12. Java Threads, using two different threads to print odd and even
number.
Program :
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaProgramsAll {
public static void main(String[] args) {
//Threads . . .
System.out.println("Program for threading");
MyThreadOdd myThreadOdd = new MyThreadOdd();
myThreadOdd.start();
MyThreadEven myThreadEven = new MyThreadEven();
myThreadEven.start();
}
}
class MyThreadOdd extends Thread {
@Override
public void run() {
super.run(); //To change body of generated methods, choose Tools | Templates.
try {
for(int i = 1; i < 11 ;i++)
{
if(i%2 == 1)
System.out.println("Running Thread ONE Printing only Odd Number " +i);
Thread.sleep(2000);
}
} catch (InterruptedException ex) {
System.out.println("Error occurred" + ex.toString());
}
}
}
class MyThreadEven extends Thread {
@Override
public void run() {
super.run(); //To change body of generated methods, choose Tools | Templates.
try {
for(int i = 1; i < 11 ;i++)
{
if(i%2 == 0)
System.out.println("Running Thread TWO Printing only even Number " +i);
Thread.sleep(2000);
19
}
}
catch (InterruptedException ex) {
System.out.println("Error occurred" + ex.toString());
}
}
}
Output :
Objective : understanding threads by sending values and creating multiple threads with different
parameters.
Program :
import java.util.logging.Level;
import java.util.logging.Logger;
Output :
Objective : understanding virtual function - creating a base class function and override base class
function in derived class
Program :
import java.net.InetAddress;
import java.net.UnknownHostException;
public class JavaProgramsAll {
public static void main(String[] args) {
System.out.println(" Java - Virtual Function \n ------------------------");
Base b = new Derived();
b.show();
}
}
class Base {
// virtual by default
public void show() {
System.out.println("Base Class function ::show() called");
}
}
class Derived extends Base {
public void show() {
System.out.println("Derived class function ::show() called");
}
}
Output :
Java - Virtual Function
------------------------
Derived class function ::show() called
23
Program :
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaProgramsAll {
public static void main(String[] args) {
//Exception handling, adding two numbers . . . .
Scanner kinput = new Scanner(System.in);
String number1, number2;
System.out.println("Program for Exception Handling");
System.out.println("------------------------------");
System.out.println("Adding two integers numbers, "
+" \n give input different inputs"
+ "\n Ex float , character, etc");
try
{
System.out.print("\nEnter the first number : ");
number1 = kinput.next();
System.out.print("\nEnter the second number : ");
number2 = kinput.next();
int result = (Integer.valueOf(number1) + Integer.valueOf(number2));
System.out.println("\nAddition of " + number1 + " and " + number2 +" is " + result);
}
catch(Exception ex)
{
System.out.println("Input Error catched is : " + ex.toString());
}
}
}
Input :
Program for Exception Handling
------------------------------
Adding two integers numbers,
give input different inputs
Ex float , character, etc
Enter the first number : 2g
Enter the second number : t6
Output :
Input Error catched is : java.lang.NumberFormatException: For input string: "2g"
24
Program :
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
Input a number :
fg
Your input fg is not a number
Exception Caught : javaprogramsall.MyException
25
Program :
Output :
Name : S.Kumar
Date of Birth : 27.10.1990
Address : No 183, Kamaraj Street
Madurai
26
18. I/O in Java - Writing into a text file using FileWriter class
Objective : understanding to write the string content into a text file using FileWriter class
Program :
import java.io.FileWriter;
import java.io.IOException;
Output :
19. I/O in Java - Writing into a text file using FileWriter class
Objective : understanding to write the string content into a text file using FileWriter class
Program :
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.applet.Applet;
import java.awt.Graphics;
public class NewApplet extends Applet {
public void init() {
}
public void paint(Graphics g) {
g.drawString("Hello applet!", 50, 25);
}
}
Output :
29
Program :
Save the following java applet program as GraphicsDemo.java, and compile it, save the compiled class
file and html file in the same directory
import java.applet.Applet;
import java.awt.*;
<html>
<body>
<applet code="GraphicsDemo.class" width="300" height="300">
</applet>
</body>
</html>
30
Program :
import java.net.InetAddress;
import java.net.UnknownHostException;
public class JavaProgramsAll {
public static void main(String[] args) {
try {
InetAddress ipAddr = InetAddress.getLocalHost();
System.out.println("This System ip address is : \n " + ipAddr.getHostAddress());
} catch (UnknownHostException ex) {
ex.printStackTrace();
}
}
}
Output :
Objective : Retrieving the host system ip address from the given website name
Program :
import java.net.InetAddress;
import java.net.UnknownHostException;
public class JavaProgramsAll {
public static void main(String[] args) {
try {
InetAddress host = InetAddress.getByName("www.w3schools.com");
System.out.println("IP Address for www.w3schools.com is : " + host.getHostAddress());
} catch (UnknownHostException ex) {
ex.printStackTrace();
}
}
}
Output :
Objective : Establish the connection between a client and a server, then send a message from client
to server
Program :
MyServer.java
import java.io.*;
import java.net.*;
String str=(String)dis.readUTF();
System.out.println("Connected with Client... \n Received message \n" + str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
}
MyClient.java
import java.io.*;
import java.net.*;
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}
Objective : Developing a text chat conversation between two systems, Two java programs running
MyServer java program at one system and MyClient java program at another system.
Program :
MyServer.java
import java.net.*;
import java.io.*;
class MyServer{
public static void main(String args[])throws Exception{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop")){
str=din.readUTF();
System.out.println("client says: "+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}}
MyClient.java
import java.net.*;
import java.io.*;
class MyClient{
public static void main(String args[])throws Exception{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
35
while(!str.equals("stop")){
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}
dout.close();
s.close();
}}
Output :
Hi Good Morning
Server says: Hello, how are yoou
I am fine
Server says: ok good bye
ok bye
Server says: Stop