CIT 135  Java 2nd mid term                          

Instruction:  Save the program in a floppy disk, turn in the floppy disk.

  1. Write a program that prompts the user to give a selection  of hamburgers from (L,M, or S).  Use the case statement to print the price of  hamburger – Larger is $1.99, Medium is $1.29, and the Small size is $0.99.

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();

        }

}

 

  1. Write a Applet program that prompts the user to give a first name, and give a last name. After the user press the button “Get Name”, the program display the user’s full name ( firstName  LastName)

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();

        }

}

       

  1. Write an Applet program that can write your name 5 times on the applet.  (Use a for loop )

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));

        }

}

 

 

  1. Write an applet program that declare and initialize an array name a of 5 elements.  The 5 elements are 10,20,30,40,50.  Display each element on the applet, display the sum of the elements as well.

 

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);

                }

}

 

  1. Write a console program that declare an integer array of 5 elements and initialize each element to 100.  Display the elements in the array .  Add 50 to each element in the array, and then display  the array again.

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;

                                                }

                }

}