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

Slides:



Advertisements
Similar presentations
Computer Programming Lab(7).
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
IT151: Introduction to Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter2: Introduction to Java Applications 1.
BUILDING JAVA PROGRAMS CHAPTER 6.4 FILE OUTPUT. 22 PrintStream : An object in the java.io package that lets you print output to a destination such as.
1 Objectives Understand streamed input and output.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Java Applications & Program Design
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Chapter 2: Java Fundamentals
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Introduction to Programming
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Introduction to programming in java
Lecture 4 – Scanner & Style
C++ First Steps.
Introduction to Java Applications
Input/Output.
Console Output, Variables, Literals, and Introduction to Type
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 2, Part I Introduction to C Programming
Chapter 2 Introduction to Java Applications
Console Input and Output
OUTPUT STATEMENTS GC 201.
Fundamentals of Java Programs, Input/Output, Variables, and Arithmetic
CSS 161 Fundamentals of Computing Introduction to Computers & Java
MSIS 655 Advanced Business Applications Programming
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Introduction to Java Applications
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

Introduction to Computing Concepts Note 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(); }

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

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”);

Print This! 6 Hello World

Print This! 7 Hello World Hello World

What would this print? 8 Hello World Hello World

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

What would this print? 10 System.out.println(“Hello\nWorld”);

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

Write Some Code 12 He said, “Go Away.” He 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.appletjava.awt java.awt.eventjava.io java.langjava.net java.utiljavax.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 the of the system A data type – Part of the API

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?

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

Breakout 3