You are on page 1of 10

PART- B PROGRAMS

Write a java program to add two numbers.

11
12 Java program to calculate the simple interest

13 Program to demonstrate the parameterized constructor

14 Program to demonstrate the method overriding


15 Program to illustrate the concept of vectors.
16 Program to demonstrate the concept of wrapper class & methods.

17 Program to create the threads using runnable interface.

18 Write a java program to implement Graphics Program to Draw CIRCLE.

19 Write a java program to implement Graphics Program to Draw RECTANGLE.

20 Write a java program to implement Graphics Program to Draw Barchat.

1
SOLUTIONS

1. Write a java program to add two numbers.

import java.util.Scanner;
public class AddTwoNumbers2 {

public static void main(String[] args) {

int num1, num2, sum;


Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
num1 = sc.nextInt();

System.out.println("Enter Second Number: ");


num2 = sc.nextInt();

sc.close();
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}

Output:
Enter First Number:
121
Enter Second Number:
19
Sum of these numbers: 140

2
2. Java program to calculate the simple interest
import java.util.Scanner;
public class JavaExample
{
public static void main(String args[])
{
float p, r, t, sinterest;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the Principal : ");
p = scan.nextFloat();
System.out.print("Enter the Rate of interest : ");
r = scan.nextFloat();
System.out.print("Enter the Time period : ");
t = scan.nextFloat();
scan.close();
sinterest = (p * r * t) / 100;
System.out.print("Simple Interest is: " +sinterest);
}
}

Output:

Enter the Principal : 2000


Enter the Rate of interest : 6
Enter the Time period : 3
Simple Interest is: 360.0

3.Program to demonstrate the parameterized constructor


class contpara

contpara(int a,int b)

3
{

System.out.println("The constructor parameters are : " + a +" and " +b);

public class java_7

public static void main(String args[])

contpara c= new contpara(10,5);

Output:

The constructor parameters are: 10 and 5

4.Program to demonstrate the method overriding


class Base
{
void type()
{
System.out.println("This is base Class");
}
}
class B1 extends Base
{
void type()
{
System.out.println("This is Class B1");

4
}
}
class B2 extends Base
{
void type()
{
System.out.println("This is Class B2");
}
}
class java13
{
public static void main(String args[])
{
B1 b1 = new B1();
B2 b2 = new B2();

b1.type();
b2.type();
}
}

Output:

This is Class B1
This is Class B2

5. Program to illustrate the concept of vectors.

import java.util.Vector;

public class VectorDemo {


public static void main(String args[])
{
Vector v = new Vector();

5
v.add("C");
v.add("C++");
v.add("JAVA");
v.add("J2EE");
System.out.println("initially the vector contents:" +v.toString());
System.out.println("the last element:" +v.lastElement());
System.out.println("the element at 2nd position:" +v.elementAt(2));
v.insertElementAt("VB",1);
v.insertElementAt("#c",0);
System.out.println("after inserting the vector contents:"
+v.toString());
v.removeElementAt(3);
System.out.println("after removing the element(3)- the contents:"
+v.toString());
v.setElementAt("C++", 1);
v.remove("VB");
System.out.println("after remove (VB) -the vector contents:"
+v.toString());

Output:

initially the vector contents:[C, C++, JAVA, J2EE]


the last element:J2EE
the element at 2nd position:JAVA
after inserting the vector contents:[#c, C, VB, C++, JAVA, J2EE]
after removing the element(3)- the contents:[#c, C, VB, JAVA, J2EE]
after remove (VB) -the vector contents:[#c, C++, JAVA, J2EE]

6. Program to demonstrate the concept of wrapper class & methods.


public class WrapperObjectsDemo {

public static void main(String[] args) {


int i=100;
Integer i1=new Integer(i);
Integer i2=Integer.valueOf("200");
System.out.println("The primitive value of i1=" +i1.intValue());
System.out.println("The primitive value of i2=" +i2.intValue());

6
String str1="12345";
int num2=Integer.parseInt(str1);
System.out.println(" the value of num 2="+num2);
System.out.println(" the string value of i1="+i1.toString());
System.out.println(" the string value of i2="+i2.toString());
}
}

Output:
The primitive value of i1=100
The primitive value of i2=200
the value of num 2=12345
the string value of i1=100
the string value of i2=200

7.Program to create the threads using runnable interface.

class demo1 implements Runnable

public void run()

System.out.println("Thread is running");

public class pgm20

public static void main(String args[])

demo1 d=new demo1();

Thread t=new Thread(d);

7
t.start();

Output:

Thread is running

8. Write a java program to implement Graphics Program to Draw CIRCLE.

import java.awt.*;

import java.applet.*;

public class test extends Applet

public void paint(Graphics g)

g.setColor(Color.red);

g.drawOval(40, 40, 50, 50);

g.fillOval(40, 40, 50, 50);

Output:

8
9. Write a java program to implement Graphics Program to Draw
RECTANGLE.

import java.awt.*;

import java.applet.*;

public class test extends Applet

public void paint(Graphics g)

g.drawRect(40, 40, 80, 50);

Output:

9
10. Write a java program to implement Graphics Program to Draw Barchart.

import java.awt.Graphics;
public class Barchart extends java.applet.Applet {
public void paint(Graphics g){
String year[]={"2006", "2007", "2008", "2009", "2010"};
int amount[]={120,140,135,150,170};
for(int i=0;i<5;i++){
g.drawString(year[i], 20, i*50+30);
g.fillRect(50, i*50+10, amount[i], 40);

Output:

10

You might also like