Upload
ronald-morgan
View
225
Download
0
Tags:
Embed Size (px)
Citation preview
Introduction to Computing ConceptsNote Set 12
Writing a Program from Scratch
public class SampleProgram1 {
public static void main (String [] args) {
System.out.println(“Hello World”);
}
}
3
/* * * Programmer: Mark Fontenot * Date: 9/3/2007 * FileName: Welcome.java * Purpose: Display Splash screen * */
public class Welcome { public static void main (String [] args) { System.out.println(); System.out.println("Welcome to my day!"); System.out.println("Daily Planner Programm");
//Will need to be changed later to System Date System.out.println("Sep 17, 2008"); System.out.println(); }}
4
main
•The method is the basic unit of work in Java•Every stand-alone java program must have at least 1
(main)•main▫The name of the method▫Where execution begins in all stand-alone Java
programs•Will be part of nearly every program you write in
1340• NOTE: I hate memorization, but it will be easier if you just memorize this
for now – we’ll learn more about the other words later
public static void main (String [] args) {
5
Output in Java
•println() is a method •You’re not writing the code to make this work▫It has already been written for you, you’re just using it▫Think about the sin function on a calculator – do you
know exactly how it works? (probably not) Does it give you the sin of a number? (We hope so)
•Can provide an argument in the form of a string literal that will be printed verbatim to the screen
•Prints argument, then goes to a new line•Statement is terminated with a semicolon (;)
System.out.println(“Some Text”);
6
Print This!
Hello World
7
Print This!Hello World
8
What would this print?Hello
World
9
Formatting Output•Escape Sequence▫Backslash followed by control character(s)▫Embedded in a string literal
Sequence Description
\nCauses the cursor to go to the beginning of the next line
\t Causes a horizontal tab to be printed
\\ Causes a backslash to be printed
\’ Causes a single quotation mark to be printed
\” Causes a double quotation mark to be printed
10
What would this print?
System.out.println(“Hello\nWorld”);
11
What would this print?
System.out.println(“\“Hello\tWorld\““);
12
Write Some CodeHe said,“Go Away.”
public class Coding1{ public static void main (String [] args) {
}}
Breakout 1 and 2
Reading Data From The User
•Using a computer would be no fun if it couldn’t take any input from the user
•We will use the scanner class from the Java API▫Allows us to read different data types from keyboard▫We’ll need to import the java.util package to help us
out
Getting Access to different “packages”•The API is wonderful – no question about that!•But it’s monstrous. Don’t need to include the whole
thing in every program• java.lang.* included by default ▫Gives you access to some fundamental parts of the API
like System.out•Must use the import statement to get access to the
part of the API that knows how to handle applet stuff•Goes before(above) class header
import – Top Level Packages
java.applet java.awt
java.awt.event java.io
java.lang java.net
java.util javax.swing
not a typo!
Creating the Scanner Object
import java.util.*;
public class ScannerTest { public static void main (String [] args) {
//Allows us to read from the keyboard Scanner keyboard = new Scanner(System.in); //more stuff here later
}}
The Scanner Object
Scanner keyboard = new Scanner(System.in);
keyboard is Scanner Object
Just an Identifier – Can have any legal identifier
Represents theof the system
A data type –Part of the API
Exampleimport java.util.*;
public class ScannerTest { public static void main (String [] args) {
int x;
Scanner keyboard = new Scanner(System.in); System.out.print("Enter an integer and I“); System.out.print(“will print it back: ");
x = keyboard.nextInt(); System.out.println(x); }}
What should the name of the file be that this class is in?
• int nextInt()▫Ignores/skips leading white space characters (space,
tab, new line)▫Begins collecting digits, stops at the first non-digit
character (leaving that character in the buffer)• char nextChar()▫Ignores/skips leading white space characters (space,
tab, new line)▫Reads next character (any valid ASCII character)
Methods for Reading data from the keyboard and their rules
Methods for Reading data from the keyboard and their Rules
•double nextDouble()▫Ignores/skips leading white space characters (space, tab, new
line)▫Begins collecting digits and a decimal point, stops at the first
non-digit character (leaving that character in the buffer•String next()▫Ignores/skips leading white space characters (space, tab, new
line)▫Collects characters and stops at the first white space
character
Methods for Reading data from the keyboard and their Rules
•String nextLine()▫Collects all characters including spaces and stops at the end
of the line
Example
int x;double p;Scanner keyboard = new Scanner(System.in); x = keyboard.nextInt( );p = keyboard.nextDouble( );
What should the name of the file be that this class is in?
Let’s Try It
•Write a program to get the length and width of a rectangle from the user. Display the Area of the rectangle to the user along with the values entered.
Enter the length: 20Enter the width: 10A 20 by 10 rectangle has area of 200
Entered by the user
Breakout 3