Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class...

25
Introduction to Computing Concepts Note Set 12

Transcript of Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class...

Page 1: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

Introduction to Computing ConceptsNote Set 12

Page 2: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

Writing a Program from Scratch

public class SampleProgram1 {

public static void main (String [] args) {

System.out.println(“Hello World”);

}

}

Page 3: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

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(); }}

Page 4: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

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) {

Page 5: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { 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”);

Page 6: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

6

Print This!

Hello World

Page 7: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

7

Print This!Hello World

Page 8: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

8

What would this print?Hello

World

Page 9: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

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

Page 10: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

10

What would this print?

System.out.println(“Hello\nWorld”);

Page 11: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

11

What would this print?

System.out.println(“\“Hello\tWorld\““);

Page 12: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

12

Write Some CodeHe said,“Go Away.”

public class Coding1{ public static void main (String [] args) {

}}

Page 13: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

Breakout 1 and 2

Page 14: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

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

Page 15: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

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

Page 16: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

import – Top Level Packages

java.applet java.awt

java.awt.event java.io

java.lang java.net

java.util javax.swing

not a typo!

Page 17: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

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

}}

Page 18: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

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

Page 19: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

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?

Page 20: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

• 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

Page 21: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

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

Page 22: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

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

Page 23: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

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?

Page 24: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

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

Page 25: Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)

Breakout 3