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

Assignment On Java Programming Practical

Uploaded by

Komal Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Assignment On Java Programming Practical

Uploaded by

Komal Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

ASSIGNMENTS

MSc. IT.

SEMESTER- I

Student Name TUYIZERE Aimable


Registration No. 14A06PIT1101
Subject Name Java Programming Practical
Subject Code PIT1
Country Name RWANDA
Faculty name Mr. Adamkani
Email ID [email protected],
[email protected]

INSTRUCTIONS:

a) Students are required to submit both the assignments

ASSIGNMENT DETAILS MARKS


Assignment A Subjective Questions 10
Assignment B Subjective Questions 15

b) Total weightage given to these assignments is 25%. ( 25 Marks)


c) The assignments are to be typed in word/pdf.
d) The two assignments are to be completed by due dates (specified from
time to time) and need to be submitted for evaluation to the University of
Madras.
e) Students have to affix a scan signature in the form.

Signature:

Date: June 8, 2015


Subject: Java Programming Practical

Answer all questions


Assignment –A

1) Write a Java program to find the given number is prime or not

ANSWER:

import java.util.Scanner;

public class Prnumber {

public boolean isPrimeNumber(int m){

for(int n=2; n<=m/2; n++){

if(m % n == 0){

return false;

return true;

public static void main(String a[]){

int input;

Prnumber fg = new Prnumber();

Scanner in = new Scanner(System.in);


System.out.println("Enter the number to check");

input = in.nextInt();

if(fg.isPrimeNumber(input))

System.out.println(input +" : is a prime number");

else

System.out.println(input +" : is not a prime number");

2) Write a Java program to find the product of two matrices


ANSWER:

import java.util.Scanner;

public class MatrixMultiplication

public static void main (String args[])

int m, n, p, q, sum = 0, c, d, k;

Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows and columns of first matrix");

m = in.nextInt();

n = in.nextInt();

int first[][] = new int[m][n];

System.out.println("Enter the elements of first matrix");

for ( c = 0 ; c < m ; c++ )

for ( d = 0 ; d < n ; d++ )

first[c][d] = in.nextInt();

System.out.println("Enter the number of rows and columns of second matrix");

p = in.nextInt();

q = in.nextInt();
if ( n != p )

System.out.println("Matrices with entered orders can't be multiplied with each other.");

else

int second[][] = new int[p][q];

int multiply[][] = new int[m][q];

System.out.println("Enter the elements of second matrix");

for ( c = 0 ; c < p ; c++ )

for ( d = 0 ; d < q ; d++ )

second[c][d] = in.nextInt();

for ( c = 0 ; c < m ; c++ )

for ( d = 0 ; d < q ; d++ )

for ( k = 0 ; k < p ; k++ )

sum = sum + first[c][k]*second[k][d];

multiply[c][d] = sum;

sum = 0;

}
}

System.out.println("Product of entered matrices:-");

for ( c = 0 ; c < m ; c++ )

for ( d = 0 ; d < q ; d++ )

System.out.print(multiply[c][d]+"\t");

System.out.print("\n");

}
3) Write a Java program to determine the order of numbers generated randomly using
Random class

ANSWER:

import java.util.Random;

import java.util.Arrays;

public class RandomOrder{

public static void main(String args []){

int [] randomNumbers = new int[10];

Random random = new Random();

for(int i =0; i<10; i++){

int randomInteger = random.nextInt(1001);

randomNumbers[i] = randomInteger;

System.out.println("Array in Random order : "+Arrays.toString(randomNumbers));


System.out.println("Array in Ascending order :
"+Arrays.toString(getAscendingOrder(randomNumbers)));

System.out.println("Array in Descending order :


"+Arrays.toString(getDescendingOrder(randomNumbers)));

public static int[] getAscendingOrder(int [] joseearray){

int i, j, temp;

for ( i = 0; i < joseearray.length - 1; i ++ )

for ( j = i + 1; j <joseearray.length; j ++ )

if( joseearray[ i ] > joseearray[ j ] )

temp = joseearray[ i ];

joseearray[ i ] = joseearray[ j ];

joseearray[ j ] = temp;

}
return joseearray;

public static int[] getDescendingOrder(int [] joseearray){

int i, j, temp;

for ( i = 0; i < joseearray.length - 1; i ++ )

for ( j = i + 1; j < joseearray.length; j ++ )

if( joseearray[ i ] < joseearray[ j ] )

temp = joseearray[ i ];

joseearray[ i ] = joseearray[ j ];

joseearray[ j ] = temp;

return joseearray;

}
}

Flow chart for sorting randomly generated numbers


4) Write a Java program to illustrate the method overloading concept

ANSWER:

public class MOverloading {

public static int average(int j1, int j2) { // A

return (j1+j2)/2;

public static double average(double j1, double j2) { // B

return (j1+j2)/2;

public static int average(int j1, int j2, int j3) { // C

return (j1+j2+j3)/3;

public static void main(String[] args) {

System.out.println(average(30, 89)); // Use A

System.out.println(average(23.7, 57.9)); // Use B

System.out.println(average(109, 20, 30)); // Use C

System.out.println(average(19.0, 25)); // Use B – int 2 implicitly casted to double 2.0


}}

5) Write a Java program to illustrate Exception handling feature

ANSWER:

import java.util.*;

public class ExceptionHandlingJosee {

public static void main(String[] args) {

int j, p, result;
Scanner input = new Scanner(System.in);

System.out.println("Input two integers");

j = input.nextInt();

p = input.nextInt();

// try block

try {

result = j/ p;

System.out.println("Result = " + result);

// catch block

catch (ArithmeticException e) {

System.out.println("Exception caught: Division by zero.");

}
Assignment-B

1) Write a Java program to create a thread using Runnable interface

ANSWER:

public class FirstThread implements Runnable

//This method will be executed when this thread is executed

public void run()

//Looping from 1 to 10 to display numbers from 1 to 10

for ( int i=1; i<=10; i++)

//Displaying the numbers from this thread

System.out.println( "Messag from First Thread : " +i);

try

Thread.sleep (1000);

catch (InterruptedException interruptedException)

/*Interrupted exception will be thrown when a sleeping or waiting

*thread is interrupted.

*/

System.out.println( "First Thread is interrupted when it is sleeping" +interruptedException);

}
}

public class SecondThread implements Runnable

//This method will be executed when this thread is executed


public void run()
{

//Looping from 1 to 10 to display numbers from 1 to 10


for ( int i=1; i<=10; i++)
{
System.out.println( "Messag from Second Thread : " +i);

try
{
Thread.sleep(1000);
}
catch (InterruptedException interruptedException)
{
/*Interrupted exception will be thrown when a sleeping or waiting
* thread is interrupted.
*/

System.out.println( "Second Thread is interrupted when it is sleeping" +interruptedException);

}
}
}
}
public class ThreadDemo

public static void main(String args[])

//Creating an object of the first thread

FirstThread firstThread = new FirstThread();

//Creating an object of the Second thread

SecondThread secondThread = new SecondThread();

//Starting the first thread

Thread thread1 = new Thread(firstThread);

thread1.start();

//Starting the second thread

Thread thread2 = new Thread(secondThread);

thread2.start();

}
Flowchart

Output

2) Write a Java program to illustrate synchronization concept in Threads

ANSWER:

Thread Synchronization program


public class ThreadSynchronization {

public static void main(String args[]) {

PrintDemo PD = new PrintDemo();

ThreadSyncDemo T1 = new ThreadSyncDemo( "Thread - 1 ", PD );

ThreadSyncDemo T2 = new ThreadSyncDemo( "Thread - 2 ", PD );

T1.start();

T2.start();

// wait for threads to end

try

T1.join();

T2.join();

catch( Exception e)

System.out.println("Interrupted");

Threat sychronizationDemo program

class ThreadSyncDemo extends Thread {

private Thread t;
private String threadName;

PrintDemo PD;

ThreadSyncDemo( String name, PrintDemo pd){

threadName = name;

PD = pd;

public void run() {

synchronized(PD) {

PD.printCount();

System.out.println("Thread " + threadName + " exiting.");

public void start ()

System.out.println("Starting " + threadName );

if (t == null)

t = new Thread (this, threadName);

t.start ();

}
Print Demo program

class PrintDemo {

public void printCount(){

try {

for(int i = 5; i > 0; i--) {

System.out.println("Counter --- " + i );

} catch (Exception e) {

System.out.println("Thread interrupted.");

Flowchart
Output

3) Write an applet program to illustrate the Frame window

ANSWER:

import java.awt.*;

import java.awt.event.*;

class JoseeFrame

public static void main(String[] args)

{ GuiFrame screen = new GuiFrame("Frame ");

screen.setSize(300,300);

screen.setVisible(true);

class GuiFrame extends Frame implements ActionListener


{

// constructor

public GuiFrame(String s)

{ super(s); //construct Frame part of Gui

setBackground(Color.cyan);

setLayout(new FlowLayout());

Button pushButton = new Button(" click to close");

add(pushButton);

pushButton.addActionListener(this);

public void actionPerformed(ActionEvent event)

if (event.getActionCommand().equals("click to close"))

System.exit(0);

}
}

Flowchart
4) Write an applet program to illustrate the Menu and Submenu controls
ANSWER:
import java.awt.*;
class AWTMenu extends Frame
{
MenuBar mbar;
Menu menu,submenu, chips;
MenuItem m1,m2,m3,m4,m5,m9,m10;

public AWTMenu()
{

// Set frame properties


setTitle("JOSEE AWT "); // Set the title
setSize(400,400); // Set size to the frame
setLayout(new FlowLayout()); // Set the layout
setBackground(Color.magenta );
setVisible(true); // Make the frame visible
setLocationRelativeTo(null); // Center the frame

// Create the menu bar


mbar=new MenuBar();

// Create the menu


menu =new Menu("Josee");
chips=new Menu("Anne");

// Create the submenu


submenu=new Menu("MSIt");

// Create MenuItems
m1=new MenuItem("math physics");
m2=new MenuItem("computer science");
m3=new MenuItem("network plus");

m4=new MenuItem("computer architecture");


m5=new MenuItem("java programing");
m9=new MenuItem("Electronics");
m10= new MenuItem("math");
// Attach menu items to menu
menu.add(m1);
menu.add(m2);
menu.add(m3);
chips.add(m9);
chips.add(m10);
// Attach menu items to submenu
submenu.add(m4);
submenu.add(m5);

// Attach submenu to menu


menu.add(submenu);

// Attach menu to menu bar


mbar.add(menu);
mbar.add(chips);
// Set menu bar to the frame
setMenuBar(mbar);
}
public static void main(String args[])
{
new AWTMenu();
}
}
Flowcharts
5) Write an applet program to draw various Graphical shapes

ANSWER:

import java.awt.*;

import java.applet.*;

/*<applet code="drawShapes.class" height=600 width=300>


*
</applet>*/

public class SomeShapes extends Applet

{
//this is 4 drawpolygon

int xs[]={40,49,60,70,57,40,35};

int ys[]={260,310,315,280,260,270,265};

//this is 4 fillpolygon

int xss[]={140,150,180,200,170,150,140};

int yss[]={260,310,315,280,260,270,265};

public void paint(Graphics g)


{

g.drawString("examples of geometrical shapes ",40,20);


g.drawLine(40,30,200,30);//x1,y1-starting point,x2,y2-ending point coordinates
g.setColor(Color.red);
g.drawString("1.Rectangles ",40,50);
g.drawLine(40,60,200,60);//x1,y1-starting point,x2,y2-ending point coordinates

g.drawRect(40,70,70,40);//x1,y1-top left corner coordinates of the rectangle.

g.fillRect(140,70,70,40);//same as above
g.setColor(Color.green);
g.drawString("2.Squares ",40,130);
g.drawLine(40,140,200,140);//x1,y1-starting point,x2,y2-ending point coordinates

g.drawRect(40,150,40,40);/*int width1-width of angle of corners int height1=height of


angle of corners*/
g.fillRect(140,150,40,40);//same
g.setColor(Color.magenta);

g.drawString("3.Ovals ",40,210);
g.drawLine(40,220,200,220);
g.drawOval(40,230,70,40);
g.fillOval(140,230,70,40);
g.setColor(Color.blue);

g.drawString("4.Circles ",40,290);
g.drawLine(40,300,200,300);
g.drawOval(40,310,40,40);
g.fillOval(140,310,40,40);
g.setColor(Color.black);

g.drawString("5.Triangle ",40,370);
g.drawLine(40,380,200,380);
g.drawLine(40,390,40,430);
g.drawLine(40,390,200,390);
g.drawLine(40,430,200,390);
g.setColor(Color.cyan);
g.drawLine(40,480,200,470);
g.drawString("6.ARC",40,470);
g.drawArc(40, 490, 75, 95, 0, 90);
g.fillArc(140, 490, 75, 95, 0, 90);

}
}

And HTML code

<HTML>
<HEAD>
<title>Hello World Applet</title>
</HEAD>
<BODY><b>
<applet codebase="c:\Users\HP\Desktop\javafile" code="SomeShapes.class" width=300
height=600>
</applet><b>
</BODY>
</HTML>
flowchart

You might also like