Transcript Lecture 8

Lecture 8
Review (Methods of Math class)
int abs( int num )
double sqrt( double num )
double pow( double num, double power )
Data type of the result Method parameters
Please take a look more Math methods at
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#method_summary
Example
2.25
double area = 2.25;
double value2 = Math.sqrt( area );
System.out.println( “value2 is “ + value2);
value2 is 1.5
Example
pow method takes two variables
(radius)2
double PI = 3.14;
double radius = 1.5;
double area = PI * Math.pow( radius, 2 );
System.out.println( “Area is “ + area);
More Methods of Math class
Please take a look more Math methods at
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#method_sum
mary
Today’s topic
• Java application
– Let’s write codes for calculator application
• Constant variable
• Talk about project 1
Write
Create
Programmer
(who we are)
Java
Program
Java
Application
Use
User
User Interface
• There are three actors
– Programmer: who writes codes for applications
– Computer: who runs applications
– User: who uses applications
• Programmers have to consider two interactions
– Programmer  Computer
– Computer  User
– So, programmers write codes for user interface
• how users interacts with applications
For example
Programmer
(who we are)
Application
User
For example
Programmer
(who we are)
User interface
Application should direct users
User
Example (MyCalculator.java)
Please create a new folder, named Example
Please download “acm.jar” file into the folder at
http://www.cs.umb.edu/~chonho/teaching/2007/files/
In Dr.Java, add acm.jar file for extra classpath
in preference window from [edit]
Create a new file, named MyCalculator.java
and save it into the folder
MyCalculator.java
Let’s start with class header and main method
public class MyCalculator
{
public static void main(String[] argv)
{
}
}
MyCalculator.java (1)
/*
* File name: MyCalculator.java
* -------------------------* This program runs as a Java application without using the
acm.program package.
*/
Comment
import acm.io.*;
import java.awt.*;
import javax.swing.*;
public class MyCalculator
{
Single line: //
Multiple lines: /* … */
Class name
It should be same as file name
MyCalculator.java (2)
public class MyCalculator
{
public static void main(String[] argv)
{
Frame title
JFrame frame = new JFrame(“My Calculator");
IOConsole console = new IOConsole();
frame.getContentPane().add(BorderLayout.CENTER, console);
Create classes
frame.setSize(500,
JFrame
300); class
of JFrame class
IOConsole Methods
class
frame.setVisible(true);
MyCalculator.java (3)
int num1, num2, total;
console.println( "This program adds two numbers.” );
println()
num1
= console.readInt( “Enter num1: “ );
same as System.out.println()
num2 = console.readInt(
“Enter num2: “ );
print out message in console
total = num1 +but,
num2;
}
}
readInt(it )receives
method 1 string parameter
ask users to
enter
antotal
integer
console.println("The
total
is " +
+ ".");number
it receives 1 string parameter
Constant variable
• Constant variable is static, and its value
does not change within entire programs.
– You can deal with it as regular variable
• Declaration of constant variable
– Regular variable
double average;
– Constant
static final double PI = 3.14;
Exercise
• 1. Mile to Kilometer conversion
• 2. Fahrenheit to Celsius conversion
• 3. Circle circumference and area
Exercise 1
• Mile to Kilometer conversion
1 Mile is 1.6 Kilometer
Create a calculator application to convert unit
from Mile to Kilometer
- Ask an user to enter one number in mile
- Calculate the unit conversion
- Print out the result in Kilometer for the user.
public class MyCalculator
{
public static void main(String[] argv)
{
JFrame frame = new JFrame(“My Calculator");
IOConsole console = new IOConsole();
frame.getContentPane().add(BorderLayout.CENTER, console);
frame.setSize(500, 300);
frame.setVisible(true);
double mile, kilo;
console.println( "This program converts mile to kilometer.” );
mile = console.readDouble( “Enter number in mile: “ );
kilo = 1.6 * mile;
console.println( mile + “ mile is " + kilo + “ kilometer");
}
}
Constant Variable
double mile, kilo;
console.println( "This program converts mile to kilometer.” );
mile = console.readDouble( “Enter number in mile: “ );
kilo = 1.6 * mile;
console.println( mile + “ mile is " + kilo + “ kilometer");
static final double MileToKilometer = 1.6;
Add this line as
an attribute in a class
double mile, kilo;
console.println( "This program converts mile to kilometer.” );
mile = console.readDouble( “Enter number in mile: “ );
kilo = MileToKilometer * mile;
console.println( mile + “ mile is " + kilo + “ kilometer");
public class MyCalculator
{
static final double MileToKilometer = 1.6;
public static void main(String[] argv)
{
JFrame frame = new JFrame(“My Calculator");
IOConsole console = new IOConsole();
frame.getContentPane().add(BorderLayout.CENTER, console);
frame.setSize(500, 300);
frame.setVisible(true);
console.println( "This program converts mile to kilometer.” );
double mile = console.readDouble( “Enter number in mile: “ );
double kilo = MileToKilometer * mile;
console.println( mile + “ mile is " + kilo + “ kilometer");
}
}
Exercise 2 (comment out previous one)
• Fahrenheit Tf to Celsius Tc conversion
Conversion formula
Tc = (5 / 9) * (Tf – 32)
Create a calculator application to convert unit
from degree Fahrenheit to Celsius
- Ask an user to enter number in Tf
- Calculate the unit conversion
- Print out the result in Tc for the user.
Exercise 3 (comment out previous one)
• Circle circumference and area
PI = 3.14
circumference = 2 * PI * radius
area = PI * (radius)2
radius
Create a calculator application to compute the
circumference and area of circle
- Ask an user to enter number for radius of circle
- Calculate the circumference and are
- Print out both results for the user.
Challenge
• If you want to do calculation twice or more
without closing windows, then you can use loop
to ask users for entering new numbers.
console.println( "This program adds two numbers.” );
for( int i=0; i<5; i++)
{
num1 = console.readInt( “Enter num1: “ );
num2 = console.readInt( “Enter num2: “ );
total = num1 + num2;
console.println("The total is " + total + ".");
}