Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)"— Presentation transcript:

1 Introduction to Computing Concepts Note Set 12

2 Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args) { System.out.println(“Hello World”); }

3 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 4 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 (;) 5 System.out.println(“Some Text”);

6 Print This! 6 Hello World

7 Print This! 7 Hello World Hello World

8 What would this print? 8 Hello World Hello World

9 Formatting Output Escape Sequence ▫ Backslash followed by control character(s) ▫ Embedded in a string literal 9 SequenceDescription \n Causes 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? 10 System.out.println(“Hello\nWorld”);

11 What would this print? 11 System.out.println(“\“Hello\tWorld\““);

12 Write Some Code 12 He said, “Go Away.” He said, “Go Away.” public class Coding1{ public static void main (String [] args) { }

13 Breakout 1 and 2

14 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

15 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

16 import – Top Level Packages java.appletjava.awt java.awt.eventjava.io java.langjava.net java.utiljavax.swing not a typo!

17 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 }

18 The Scanner Object Scanner keyboard = new Scanner(System.in); keyboard is Scanner Object Just an Identifier – Can have any legal identifier Represents the of the system A data type – Part of the API

19 Example import 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?

20 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

21 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

22 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

23 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?

24 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: 20 Enter the width: 10 A 20 by 10 rectangle has area of 200 Entered by the user

25 Breakout 3


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

Similar presentations


Ads by Google