Download Lab 13

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lab 14
Note: In order to run these applets please use an IDE like jGRASP which runs
applets directly. HTML is not introduced in this lab.
Lesson 142:
Exercise 1
to 6
An example of the applet is:
import java.awt.*;
// Supplies layout manager
import java.awt.event.*; // Supplies event classes
import java.applet.Applet; // Supplies class Applet
public class OddEven extends Applet implements ActionListener
{
public void actionPerformed(ActionEvent event)
// Event handler method
{
int number;
number = Integer.parseInt(inputField.getText());
System.out.println("Enter a three digit number. ");
if ((number / 2) * 2 == number)
outLabelUnits.setText("Unit's position is even.");
else
outLabelUnits.setText("Unit's position is odd.");
if ((((number / 10) / 2) * 2) == (number / 10))
outLabelTens.setText("Ten's position is even.");
else
outLabelTens.setText("Ten's position is odd.");
if ((((number / 100) / 2) * 2) == (number / 100))
outLabelHundreds.setText("Hundred's position is even.");
else
outLabelHundreds.setText("Hundred's position is odd.");
inputField.setText("");
}
// Declaring fields
private TextField inputField;
private Button button;
private Label outLabelUnits, outLabelTens, outLabelHundreds;
public void init()
{
Label label;
label = new Label("Enter a three digit number and click enter button");
outLabelUnits = new Label("Units");
outLabelTens = new Label("Tens");
outLabelHundreds = new Label("Hundreds");
button = new Button("enter");
button.addActionListener(this);
inputField = new TextField("Value here");
// Add window compontents
add(label);
add(inputField);
add(button);
add(outLabelHundreds);
add(outLabelTens);
add(outLabelUnits);
setLayout(new GridLayout(6,0));
}
}
Lesson 143:
Exercise 1
The applet solution allows the user to run the calculation
repeatedly, until they close the window.
An example of the applet is:
import java.awt.*;
// Supplies layout manager
import java.awt.event.*; // Supplies event classes
import java.applet.Applet; // Supplies class Applet
public class IntegerRead extends Applet implements ActionListener
{
public void actionPerformed(ActionEvent event)
// Event handler method
{
String input;
input = inputField.getText();
boolean valid = true;
// test for valid input
for (int position = 0; position < input.length(); position++)
if (!Character.isDigit(input.charAt(position))) valid = false;
if (valid && (input.length() > 0))
{ // valid integer and non empty string
if (input.length()/2 * 2 == input.length())
outLabel.setText("the integer has an even number of digits");
else
outLabel.setText("the integer has an odd number of digits");
}
else // invalid or empty entry
outLabel.setText("Please enter a valid integer");
inputField.setText("");
}
// Declaring fields
private TextField inputField;
private Button button;
private Label outLabel;
public void init()
{
Label label;
label = new Label("Enter an integer and click enter button");
outLabel = new Label("odd or even?");
button = new Button("enter");
button.addActionListener(this);
inputField = new TextField("Value here");
// Add window compontents
add(label);
add(inputField);
add(button);
add(outLabel);
setLayout(new GridLayout(4,0));
}
}
Exercise 2
Test cases included:
A valid integer with an odd number of digits 123
A valid integer with an even number of digits 4567
An invalid number 123.45
An invalid number qwer5
An empty number (just clicking enter)
An example of the applet is:
import java.awt.*;
// Supplies layout manager
import java.awt.event.*; // Supplies event classes
import java.applet.Applet; // Supplies class Applet
public class StringReverse extends Applet implements ActionListener
{
public void actionPerformed(ActionEvent event)
// Event handler method
{
String input;
String reverseInput;
input = inputField.getText();
char [] inputChar = input.toCharArray();
char [] inputCharReverse = new char [inputChar.length];
// test for valid input
for (int position = inputChar.length-1; position >= 0; position--)
inputCharReverse[inputChar.length-1-position]
= inputChar[position];
reverseInput = String.valueOf(inputCharReverse);
outLabel.setText("Reversed String is : " + reverseInput);
inputField.setText("");
}
// Declaring fields
private TextField inputField;
private Button button;
private Label outLabel;
public void init()
{
Label label;
label = new Label("Enter a string and click enter button");
outLabel = new Label("reverse order string is");
button = new Button("enter");
button.addActionListener(this);
inputField = new TextField("String here");
// Add window compontents
add(label);
add(inputField);
add(button);
add(outLabel);
setLayout(new GridLayout(4,0));
}
}
Exercise 3
An example of the applet is:
import java.awt.*;
// Supplies layout manager
import java.awt.event.*; // Supplies event classes
import java.applet.Applet; // Supplies class Applet
public class DateFormat extends Applet implements ActionListener
{
public void actionPerformed(ActionEvent event)
// Event handler method
{
String day;
String month;
String year;
day = inputDay.getText();
month = inputMonth.getText();
year = inputYear.getText();
outLabelUS.setText("US: " + month + " " + day + " " + year);
outLabelEng.setText("English: " + day + " " + month + " " + year);
inputDay.setText("");
inputMonth.setText("");
inputYear.setText("");
}
// Declaring fields
private TextField inputDay;
private TextField inputMonth;
private TextField inputYear;
private Button button;
private Label outLabelUS, outLabelEng;
public void init()
{
Label label, labelDay, labelMonth, labelYear;
label = new Label("");
labelDay = new Label("Enter the day");
labelMonth = new Label("Enter the month");
labelYear = new Label("Enter a year and click enter button");
outLabelUS = new Label("US date format");
outLabelEng = new Label("English date format");
button = new Button("enter");
button.addActionListener(this);
inputDay = new TextField("Day here");
inputMonth = new TextField("Month here");
inputYear = new TextField("Year here");
// Add window compontents
add(label);
add(button);
add(labelDay);
add(inputDay);
add(labelMonth);
add(inputMonth);
add(labelYear);
add(inputYear);
add(outLabelUS);
add(outLabelEng);
setLayout(new GridLayout(5,2));
}
}
Lesson 144 and 14-5
Please ignore these lab components. Building web pages is
included in a later course.
Related documents