Java Applications

import java.io.*;

public class FirstApp
{

public static int wait;

public static void main( String args[] ) throws IOException
{
int age;
age = 12;
String name = "Bill";
System.out.println(name + " is " + String.valueOf(age) + " years old." );

wait = System.in.read(); //used to make the output stay on the screen
}
}

A black DOS screen will appear (the Java interpreter program in Windows) and the program will run, outputting the string Bill is 12 years old. Look at the program to make sense of this output. Change the code to print Sam is 48 years old. Try to add a second line of text.

What is happening? A Java applet runs by first entering the init method (if any), the start method (if any) and then the paint method (again,,,). Finally, it waits for events caught by the action/actionPerformed methods, depending on the Java version you are running. For an application, only one method is called.. the method main- just like C or C++. The header for main will always be a copy for you.. do not worry about String args[] or IOException.. only that they have to be here. The first allows you to pass parameters to your program and the second catches IO errors for you. Why and How?? Just Wait...

The last line in the program, wait = System.in.read(); is used to keep the output on the screen until the user presses a key. Note System is a class (Why?) in.read() gets a character from the keyboard. More on this later. The rest of the program should look a bit familier. Now lets go to a new example. This one will be the template you will use when you write the Java application for the test. I suggest you print it out and study it carefully. Copy the following:

/* This is a template program that you will need to know
in order to do the programming test. The idea
of this test is to demonstrate your knowledge and application
of basic programming and basic java language features. There
are several parts that your program must have. You should
study these and be able to give them back on a test. The
pieces you will need are: 1: The header for your program 2:
declaring your variables, including the input stream 3: Opening
the input stream 4: getting the variables 5: Basic flow
of control statements (if and while) 6: Output

Number 1: the header */

import java.io.*;

public class TrialTest
{
public static void main( String args[] ) throws IOException
{

/* this is the header you will always use, except
you will have your name for the class, not TrialTest */

DataInputStream input;
String name;
float gpa;
int age, wait;

/* 2: This is how you declare variables. You will need
to be able to use each of these types of variables/objects
Note the first word is the primative data type (int
and float) or the Class and the second name is your
identifier name. In some cases you may need to initialize
your variables as below: */
age = 0;

/* 3: Opening the Input Stream. Use this line, except
input can be your identifier name. */

input = new DataInputStream(System.in);

/* 4: Getting the variables. Below is the code to
input a String, integer, and a float number. The only
thing that will change is your identifier names */

System.out.println("Enter name:");
name = input.readLine();

System.out.println( "Enter the age for " + name);
age = Integer.parseInt(input.readLine());

System.out.println("Enter the GPA for " + name);
gpa = Float.parseFloat(input.readLine());

/* 5: Basic flow of control. This code will be VERY
similar to the code you have already written in BASIC,
C, Pascal, etc.. You will use the java if, while, and
assignment operators to solve the problem */

while (age<99)
{
System.out.println( "Enter age -in loop ");
age = Integer.parseInt(input.readLine());
}

/* 6: Output. The code below shows how
to give basic output. Note it is in String form. */

System.out.println("name " + name + "age " + age + "gpa " + String.valueOf(gpa));
wait = System.in.read(); //used to make the output stay on the screen

}
}


Note the /* and */ comments.. Anything between them will be ignored by the compiler. Read the comments. Most of this code will be copied for any application. You obviously can create, initialize and input as many data values/objects as you want.

TASK:

Redo the Applet Input and Calculations program, only write them as an Application. Add these programs to your portfolio.

 

BuiltWithNOF

[Java at Evergreen] [Syllabus] [To Do List] [Lessons/Programs] [Evaluation/Review] [Help] [FAQ/GWIFO] [Discuss/Reports] [Resources] [Readings] [Portfolio] [Instructor]