Download presentation
Presentation is loading. Please wait.
1
Review
3
By the end of today you should be able to- Know how to use args Know how to use the JOptionPane Know how to convert a String to a number
4
What goes through the door?
5
Input into a program Is always a.... String
6
Input is always a String
7
args { public static void main(String[] args) { String name; name = args[0]; System.out.println(name); } Where is the door? Where is the room?
8
args – This is the same as the previous slide { public static void main(String[] args) { String name = args[0]; System.out.println(name); } Where is the door? Where is the room?
9
args { public static void main(String[] args) { String name, age; name = args[0]; age = args[1]; System.out.println(name + age); } Where is the door? Where is the room?
10
args - This is the same as the previous slide { public static void main(String[] args) { String name = args[0]; String age = args[1]; System.out.println(name + age); } Where is the door? Where is the room?
11
JOptionPane 01 import javax.swing.*; 02 class HelloAge2 03 { 04 public static void main(String[] args) 05 { 06 String name = JOptionPane.showInputDialog(“Type your name"); 07 String ageStr = JOptionPane.showInputDialog(“Type your age"); 08 JOptionPane.showMessageDialog 09 (null, "Hello " + age + " year old " + name); 10 } 11 } Where is the door? Where is the room?
12
JOptionPane - This is the same as the previous slide 01 import javax.swing.*; 02 class HelloAge2 03 { 04 public static void main(String[] args) 05 { 06 String nameStr, ageStr; 07 nameStr = JOptionPane.showInputDialog(“Type your name"); 08 ageStr = JOptionPane.showInputDialog(“Type your age"); 09 JOptionPane.showMessageDialog 10 (null, "Hello " + age + " year old " + name); 11 } 12 } Where is the door? Where is the room?
13
Convert the String of numbers if you want to use them
15
Now you can perform mathematical operations 1 + 2 = 3 int or double 2 * 8 = 16 int or double 7 / 5 = 1.4 int or double
16
So how do you convert from String to number? int age = Integer.parseInt(ageStr); To convert a string into a whole number (integer) -
17
So how do you convert from String to number? double hours = Double.parseDouble(hoursStr); To convert a string into a fractional number (double) -
18
Question For You Write a program to read a number and print out the area of a circle of that radius. Assume that the value of pi is 3.14 The area of a circle is pi x r 2 Your output should take the form: The area of a circle of radius... is.... units. Use args and then repeat the questions using JOptionPane --------------------------------- First, interpret what the question is asking. Your program takes a number as an input from the user. This will be the radius. Next calculate the area of the circle. Finally print out the answer in the format given- The area of a circle of radius... is.... units.
19
Steps to take to write the program 1.Decide on how the user will input the radius into the program. First use args, then repeat using JOptionPane 2.All input is a string. So convert the input into a number. 3.Decide on what type of number we are dealing with, is it int or double? Pi is a fractional number, so use double. 4.Now we have the radius, so we can next calculate the area. 5.Now print the answer
20
Copy the code at the bottom, and paste into EditPlus. Comment each line, and place into your log book. import javax.swing.*; class AreaOfCircle { public static void main(String[] args) { String radiusStr; radiusStr = JOptionPane.showInputDialog("Type the radius"); double radius = Double.parseDouble(radiusStr); double area = 3.14 * radius * radius; JOptionPane.showMessageDialog (null,"The area of a circle of radius " + radius + " is " + area + " units."); } Notice the import
21
Copy the code at the bottom, and paste into EditPlus. Comment each line, and place into your log book. class AreaOfCircle { public static void main(String[] args) { String radiusStr; radiusStr = args[0]; double radius = Double.parseDouble(radiusStr); double area = 3.14 * radius * radius; System.out.println("The area of a circle of radius " + radius + " is " + area + " units."); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.