Review. 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.

Slides:



Advertisements
Similar presentations
Question from the last class What happens if we cast “too large” float/double to an int? int has range float a=1e10f; int b=(int)
Advertisements

1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Hand Crafting your own program By Eric Davis for CS103.
Laboratory 4.  This algorithm reads two numbers, A and B and prints the largest value and the samllerest value.  [Input data values] Read(A,B)  [Determine.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
Lecture 8. Review (Methods of Math class) int abs( int num ) double sqrt( double num ) double pow( double num, double power ) Method parametersData type.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Computer Programming Lab(4).
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Computer Programming Lab(5).
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
Introduction to Objects A way to create our own types.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
1 BUILDING JAVA PROGRAMS CHAPTER 6 DETAILS OF TOKEN-BASED PROCESSING.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
Indentation & Readability. What does this program do? public class Hello { public static void main ( String[] args ) { //display initial message System.out.println(
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Cumulative algorithms. 2 Adding many numbers How would you find the sum of all integers from ? // This may require a lot of typing int sum = 1 +
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
JOptionPane. Import javax.swing.JOptionPane Use showInputDialog() for input. Only string values can be input. To convert an input value from a string.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Input, Output and Variables GCSE Computer Science – Python.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
using System; namespace Demo01 { class Program
Elementary Programming
AP Java 10/4/2016.
Chapter 2.
Chapter 3: Introduction to Objects and Input/Output
Java Intro.
AP Java 10/4/2016.
Code Animation Examples
class PrintOnetoTen { public static void main(String args[]) {
JOptionPane class.
F II 2. Simple Java Programs Objectives
Random Numbers while loop
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Review

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

What goes through the door?

Input into a program Is always a.... String

Input is always a String

args { public static void main(String[] args) { String name; name = args[0]; System.out.println(name); } Where is the door? Where is the room?

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?

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?

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?

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?

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?

Convert the String of numbers if you want to use them

Now you can perform mathematical operations = 3 int or double 2 * 8 = 16 int or double 7 / 5 = 1.4 int or double

So how do you convert from String to number? int age = Integer.parseInt(ageStr); To convert a string into a whole number (integer) -

So how do you convert from String to number? double hours = Double.parseDouble(hoursStr); To convert a string into a fractional number (double) -

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.

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

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

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."); }