CIT 135 Java 2nd mid term
Instruction:
Save the program in a floppy disk, turn in the floppy disk.
public class f1
{
public static void main (String[] args)throws Exception
{
char choose;
System.out.println("please enter number purchased");
choose =(char)System.in.read();
System.in.read();
System.in.read();
switch(choose)
{
case 'L':
System.out.println("1.99");
break;
case 'M':
System.out.println("1.29");
break;
case 'S':
price = 0.99 * number;
System.out.println("0.99");
break;
}
System.in.read();
System.in.read();
System.in.read();
}
}
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class f4 extends Applet implements ActionListener
{
Label fn,ln,namelabel;
TextField first, last;
Button getName;
public void init()
{
fn = new Label("Enter first name");
ln = new Label("Enter your last name");
first = new TextField(" ",20);
last = new TextField(" " , 20);
getName = new Button("GetName");
namelabel = new Label(" ");
add(fn);
add(first);
add(ln);
add(last);
add(getName);
getName.addActionListener(this);
first.addActionListener(this);
last.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String name = " ";
String firstName = first.getText();
String lastName = last.getText();
name = firstName + " " + lastName;
namelabel.setText(name);
add(namelabel);
invalidate();
validate();
}
}
Be careful for the x and y position.
import java.awt.*;
import java.applet.*;
public class f5 extends Applet
{
public void paint(Graphics g)
{
int ypos = 50;
for (int i = 0; i <5; i++)
g.drawString("Shin Liu", 25 , ypos+(i*25));
}
}
Expect output should look like the following:
The elements are :
10 20 30 40 50
The sum of the elements is
150
import java.applet.*;
import java.awt.*;
public class F4 extends Applet
{
public void paint (Graphics g)
{
int[] numbers={10, 20, 30, 40, 50};
String header1=new String("The elements are:");
String header2=new String("The sum of the elements is:");
g.drawString(header1, 30, 40);
int sum=0;
int dis=30;
for(int i=0; i<5; i++)
{
g.drawString(String.valueOf(numbers[i]), dis, 60);
dis+=30;
sum += numbers[i];
}
g.drawString(header1, 30, 80);
g.drawString(String.valueOf(sum), 30, 100);
}
}
The output should look like the following:
The elements in the array are;
100 100 100 100 100
The elements of the array are:
150 150 150 150 150
import java.applet.*;
import java.awt.*;
public class F5 extends Applet
{
public void paint (Graphics g)
{
int[] numbers={100, 100, 100, 100, 100};
String header1=new String("The elements in the array are:");
String header2=new String("The elements of the array are:");
g.drawString(header1, 30, 40);
int dis=30;
for(int i=0; i<5; i++)
{
g.drawString(String.valueOf(numbers[i]), dis, 60);
dis+=40;
}
for(int i=0; i<5; i++)
numbers[i] += 50;
g.drawString(header2, 30, 100);
dis=30;
for(int i=0; i<5; i++)
{
g.drawString(String.valueOf(numbers[i]), dis, 120);
dis+=40;
}
}
}