Someone please help how to solve this JAVA programs

a)An applet that does the following. Takes as input three numbers, computes two numbers and prints these two numbers (interest and amount) out. Have three textfields to input the three numbers (principal, rate and the number of years). With three five top-level variables: simpleInterest, amount, principal, numYears, rate.

B)program that will have the buttons one, two and three run from left to right along the top of the frame

C) Program that tosses a coin several times until head it obtains 10 heads. it should also record number of tails.

Thanks

a)An applet that does the following. Takes as input three numbers, computes two numbers and prints these two numbers (interest and amount) out. Have three textfields to input the three numbers (principal, rate and the number of years). With three five top-level variables: simpleInterest, amount, principal, numYears, rate.

B)program that will have the buttons one, two and three run from left to right along the top of the frame

C) Program that tosses a coin several times until head it obtains 10 heads. it should also record number of tails.

Thanks

View Answers

April 17, 2010 at 3:55 PM

Hi Friend,

a)

AppletForm.java

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class AppletForm extends Applet implements ActionListener {

Label l1,l2,l3,l4,l5;

TextField t1,t2,t3;

Button b;

Panel p;

public void init(){

l1=new Label("Principal");

l2=new Label("Rate Of Interest (in %)");

l3=new Label("Number of years");

l4=new Label();

l5=new Label();

t1=new TextField(20);

t2=new TextField(20);

t3=new TextField(20);

p=new Panel(new GridLayout(5,2));

b = new Button("Calculate");

b.addActionListener(this);

p.add(l1);

p.add(t1);

p.add(l2);

p.add(t2);

p.add(l3);

p.add(t3);

p.add(l4);

p.add(l5);

p.add(b);

add(p);

}

public void actionPerformed(ActionEvent e){

double principal=Double.parseDouble(t1.getText());

double rate=Double.parseDouble(t2.getText());

double years=Double.parseDouble(t3.getText());

double simpleInterest=principal*rate*years/100;

double amount=simpleInterest+principal;

l4.setText("Simple Interest= "+Double.toString(simpleInterest));

l5.setText("Amount= "+Double.toString(amount));

}

}

a)

AppletForm.java

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class AppletForm extends Applet implements ActionListener {

Label l1,l2,l3,l4,l5;

TextField t1,t2,t3;

Button b;

Panel p;

public void init(){

l1=new Label("Principal");

l2=new Label("Rate Of Interest (in %)");

l3=new Label("Number of years");

l4=new Label();

l5=new Label();

t1=new TextField(20);

t2=new TextField(20);

t3=new TextField(20);

p=new Panel(new GridLayout(5,2));

b = new Button("Calculate");

b.addActionListener(this);

p.add(l1);

p.add(t1);

p.add(l2);

p.add(t2);

p.add(l3);

p.add(t3);

p.add(l4);

p.add(l5);

p.add(b);

add(p);

}

public void actionPerformed(ActionEvent e){

double principal=Double.parseDouble(t1.getText());

double rate=Double.parseDouble(t2.getText());

double years=Double.parseDouble(t3.getText());

double simpleInterest=principal*rate*years/100;

double amount=simpleInterest+principal;

l4.setText("Simple Interest= "+Double.toString(simpleInterest));

l5.setText("Amount= "+Double.toString(amount));

}

}

April 17, 2010 at 4:04 PM

continue..

2)ButtonForm.java:

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class ButtonForm extends Applet{

Button b1,b2,b3;

Panel p;

public void init(){

p=new Panel();

b1 = new Button("One");

b2 = new Button("Two");

b3 = new Button("Three");

p.add(b1);

p.add(b2);

p.add(b3);

add(p);

}

}

3)Toss.java:

class Toss{

public final int HEADS = 0;

static int countH=0;

static int countT=0;

private static int face;

public static void flip(){

face = (int) (Math.random() * 2);

}

public String toString(){

String faceName;

if (face == HEADS){

faceName = "Heads";

countH++;

}

else{

faceName = "Tails";

countT++;

}

return faceName;

}

public static void main (String[] args) {

do{

flip();

System.out.println(new Toss().toString());

}

while (countH!=10);

System.out.println("Number of Tails: "+countT);

}

}

Thanks

2)ButtonForm.java:

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class ButtonForm extends Applet{

Button b1,b2,b3;

Panel p;

public void init(){

p=new Panel();

b1 = new Button("One");

b2 = new Button("Two");

b3 = new Button("Three");

p.add(b1);

p.add(b2);

p.add(b3);

add(p);

}

}

3)Toss.java:

class Toss{

public final int HEADS = 0;

static int countH=0;

static int countT=0;

private static int face;

public static void flip(){

face = (int) (Math.random() * 2);

}

public String toString(){

String faceName;

if (face == HEADS){

faceName = "Heads";

countH++;

}

else{

faceName = "Tails";

countT++;

}

return faceName;

}

public static void main (String[] args) {

do{

flip();

System.out.println(new Toss().toString());

}

while (countH!=10);

System.out.println("Number of Tails: "+countT);

}

}

Thanks

Related Tutorials/Questions & Answers: