Showing posts with label bcsl-043. Show all posts
Showing posts with label bcsl-043. Show all posts

Tuesday 3 February 2015

Question 1: Write a Java program to show use of all the logical operators in Java.
Solution:

package operatorsdemo;

public class BitwiseLogicalOpDemo {

public static void main(String[] args) {

//Integer bitwise logical operator

int a = 65; // binary representation 1000001

int b = 33; // binary representation 0100001

System.out.println("a & b= " + (a & b));

System.out.println("a | b= " + (a | b));

System.out.println("a ^ b= " + (a ^ b));

System.out.println("~a= " + ~a);

//boolean logical operator

boolean bool1 = true;

boolean bool2 = true;

boolean bool3 = false;

System.out.println(“bool1 & bool2= ” + (bool1 & bool2));

System.out.println(“bool2 & bool3= ” + (bool2 | bool3));

System.out.println(“bool2 | bool3= ” + (bool2 | bool3));

System.out.println(“bool1 ^ bool2= ” + (bool1 ^ bool2));

System.out.println(“!bool1= ” + !bool1);

}
}

Question 2: Write a Java program to create Shape class. Define proper constructors and functions to create shape class objects and find its area. Inherit Circle and Square classes from Shape class and override method of area calculation.
Solution:

package com.java2novice.algos;

public class StringRecursiveReversal {

String reverse = "";

public String reverseString(String str){

int strlen=str.length();

System.Out.println(“The Length of String”+strlen);

if(str.length() == 1)

{

return str;

}

else

{

reverse += str.charAt(str.length()-1)

+reverseString(str.substring(0,str.length()-1));

return reverse;

}
}

public static void main(String a[]){

StringRecursiveReversal srr = new StringRecursiveReversal();

System.out.println("Result: "+srr.reverseString("Java2novice"));

}

}

Question 3: Write a program in Java to find the length of a given string. Also write a method in this program to display the string in reverse order.
Solution:

public abstract class Shape {

public Shape(String id)

{

this.id=id;

}

public abstract double getArea();

public String getId()

{

return id;

}

public String toString( )

{

return "Shape[id="+id+",area="+getArea()+"]";

}

private String id;

}

Save File as Shape.java

public class Sqaure extends Shape {

public Sqaure (String name, double w, double h)

{

super(name);

width = w;

height = h;

}

//Overide the abstract method declared in Shape

public double getArea()
{return width*height;}


public double getWidth()
{return width;}

public double getHeight()
{return height;}

public void setWidthHeight(double newWidth, double newHeight)

{

width = newWidth;

height = newHeight;

}

public String toString( )
{return "Sqaure[width="+width+",height="+height+","+super.toString()+"]";}

private double width, height;

}

Saves File as Sqaure.java

public class Circle extends Shape {

public Circle (String name, double r)

{

super(name);

radius = r;

}

//Overide the abstract method declared in shape

public double getArea()
{return Math.PI * radius * radius;}

public double getRadius()
{return radius;}

public void setRadius(double newRadius)
{radius = newRadius;}

public String toString( )
{return “Circle[radius="+radius+","+super.toString()+"]“;}

private double radius;

}

Saves Files as Circle.java

public class TestShape {

public static void main(String argv[]) {

Shape s1 = new Shape();

s1.Sqaure(“SQAURE”, 4, 5);

System.out.println(s1);

System.out.println(“Area is ” + s1.getArea());

Shape s2 = new Shape();

s2.Circle(“CIRCLE”, 15);

System.out.println(s2);

System.out.println(“Area is ” + s2.getArea());

Shape s3 = new Shape();

s3.Shape(“green”);

System.out.println(s3);

System.out.println(“Area is ” + s3.getArea());

}
}

Saves the File As TestJava.java

Question 4: Write a program in Java to create an applet which find the interest earned on a given principal amount for a given specified period ( in months), assuming that interest rate is 15% per annum.
Solution:

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/*<applet title="Java(TM)" code="bcsl43" width="400" height="200"> </applet>*/

public class bcsl43 extends Applet implements ActionListener

{

String str=”THE AMOUNT IS”;

float amount;

Button b1;

Label l1,l2,l3;

TextField t1,t2,t3;

public void init()

{

setBackground(Color.magenta);

setForeground(Color.blue);

l1=new Label(“PRINCIPAL”);

t1=new TextField(6);

l2=new Label(“RATE “);

t2=new TextField(6);

l3=new Label(“MONTHS”);

t3=new TextField(6);

add(l1);

add(t1);

add(l2);

add(t2);

add(l3);

add(t3);

b1=new Button(“CALCULATE”);

add(b1);

b1.addActionListener(this);

}

public void actionPerformed(ActionEvent e)

{

int pri=Integer.parseInt(t1.getText().trim());

int months=Integer.parseInt(t2.getText().trim());

int rate=15;

amount=(pri*months*rate)/100;

repaint();

}

public void paint(Graphics g)

{

Font font=new Font(“Monotype corsiva”,Font.BOLD,30);

g.setFont(font);

FontMetrics fm=g.getFontMetrics();

g.setColor(Color.GREEN);

g.drawString(” THE AMOUNT “+amount,40,100);

}
}

After Compiling the Java Code the Created Class can use in Web page.

<HTML>
<HEAD>
</HEAD>
<BODY>
<div >
<APPLET CODE="bcsl43.class" WIDTH="800" HEIGHT="500">
</APPLET>
</div>
</BODY>
</HTML>