AP Java 10/4/2016.

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

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.
Chapter 2 - Introduction to Java Applications
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 3 Variables Primitive Data Types calculations & arithmetic operators Readings: Chapter 2.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
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.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2008.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
IC211 Lecture 8 I/O: Command Line And JOptionPane.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Chapter 2 - Introduction to Java Applications
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2010.
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.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
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.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
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 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
1/23: Java Modular Components Identifiers: naming requirements for Java variables & objects Stepping out of the MS-DOS window: The Java Modular Component:
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.
Dialog Boxes.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
InterestRate Create an InterestRate class and InterestRateViewer client class to do the following: A person is purchasing an item with their credit card.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
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.
CS 201 Lecture 7: John Hurley Summer 2012 Cal State LA.
Data Types – Reference Types
Introduction to Classes and Objects
John Hurley CS201 Cal State LA
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
AP Java 10/4/2016.
2.5 Another Java Application: Adding Integers
Section 64 – Manipulating Data Using Methods – Java Swing
Chapter 3 Java Input/Output.
Advanced Programming Lecture 02: Introduction to C# Apps
Chapter 2 - Introduction to Java Applications
Truth tables: Ways to organize results of Boolean expressions.
AP Java Review If else.
Truth tables: Ways to organize results of Boolean expressions.
Chapter 3 Input/Output.
Object Oriented Programming
AP Java Review If else.
CS431 ws99 Half Text Half Graphics
JOptionPane class.
F II 2. Simple Java Programs Objectives
Random Numbers while loop
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

AP Java 10/4/2016

Learning Objectives Be able to use Java’s Swing class to make windows applications Be able to convert from String to int and double. Be able to use Math.random() and type casting to generate ranges of random numbers.

string a = "infinity"; 1. ) int b = a. length() - 4;. b = 2 string a = "infinity"; 1.) int b = a.length() - 4; b = 2.) int c = a.indexOf("ty") + b; c = 3.) boolean d = !a.equals("infinity"); d = 4.) String e = a.substring(4); e = 5.) String f = a.substring(1, 8); f = 6.) boolean g = a.equals("infInity"); g = 7.) int h = a.indexOf("z"); h = 8.) int i = a.length() - b/g; i = 9.) int j = a.substring(2); j = 10.) int k = a.indexOf("ini") * c; k =

Basic Input and Output using Java Swing.

GUI 101 with javax.swing // Fig. 3.17: Dialog1.java Look at java docs help files for JOptionPane to find out additional methods. // Fig. 3.17: Dialog1.java // Printing multiple lines in dialog box. import javax.swing.JOptionPane; public class Dialog1 { public static void main( String args[] ) // display a dialog with the message JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); } // end main } // end class Dialog1

Input with swing // Fig. 3.18: NameDialog.java // Basic input with a dialog box. import javax.swing.JOptionPane; public class NameDialog { public static void main( String args[] ) // prompt user to enter name String name = JOptionPane.showInputDialog( "What is your name?" ); // create the message String message = String.format( "Welcome, %s, to Java Programming!", name ); // display the message to welcome the user by name JOptionPane.showMessageDialog( null, message ); } // end main } // end class NameDialog Input with swing

Swing input stuff It only enters a String, not a double or int. So to enter numbers you will need to convert them to integers or doubles To convert a String into an integer. int answer=Integer.parseInt(name); To convert to a double: double answer=Double.parseDouble(name);

String For int, import javax.swing.JOptionPane; public class NameDialog { public static void main( String [] args) // prompt user to enter name String name = JOptionPane.showInputDialog( "What is your name?" ); //Prompt for the age String ageString =JOptionPane.showInputDialog( "What is your age?" ); int age = Integer.parseInt(ageString); // create the message String message = String.format( "Welcome, %s, to Java Programming! \n %d", name, age ); // display the message to welcome the user by name JOptionPane.showMessageDialog( null, message ); } // end main } // end class NameDialog String For int, Use %f for double

Swing Program Tree height calculator: Resisting Change Input the distance you are from a tree and the angle you look up to the top of the tree. Output: The height of the tree. Calculation: tree height = the height of your eyes + (Distance from the tree) * tan(angle) Look up the Math.tan() method to determine if the angle is in degrees or radians. Push: Research to find out how to use this information to estimate the number of ‘board-foot’ volume of the standing tree. Will you need any other information? Resisting Change Input: The resistance in Ohms of two resistors wired in parallel (r1 and r2). Output: The net resistance. NetResistance = r1*r2 / (r1 + r2) Push: Be able to find the resistance given three resisters connected in parallel. Push: Be able to find the resistance of n resistors connected in parallel. Let the user enter the number of resisters.