Introduction to Java Import Scanner class to use in our program

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Chapter 1: Computer Systems
INTRODUCTION Chapter 1 1. Java CPSC 1100 University of Tennessee at Chattanooga 2  Difference between Visual Logic & Java  Lots  Visual Logic Flowcharts.
Chapter 1: Introduction
A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
How to Create a Java program CS115 Fall George Koutsogiannakis.
The switch statement: an N-way selection statement.
Introducing Java.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
From BlueJ to NetBeans SWC 2.semester.
Java: Chapter 1 Computer Systems Computer Programming II.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
CS107 Introduction to Computer Science Java Basics.
UCSC All rights reserved. No part of this material may be reproduced and sold. 1 IT1202-Fundamentals Of Programming (Using JAVA) Interacting with.
The Java Programming Language
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
How to Run a Java Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
EIE375 BlueJ: Getting Started Dr Lawrence Cheung.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
CS116 COMPILER ERRORS George Koutsogiannakis 1. How to work with compiler Errors The Compiler provide error messages to help you debug your code. The.
1/10/2008. >>> About Us Paul Beck * Third quarter TA * Computer Engineering * Ryan Tucker * Second quarter TA * Computer.
Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging using eclipse The Java API.
CS 177 Recitation Week 1 – Intro to Java. Questions?
CS 201 Lecture 1 (b) Using an IDE Tarik Booker CS 201: Introduction to Programming California State University, Los Angeles.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Computer Programming Your First Java Program: HelloWorld.java.
CompSci 230 S Programming Techniques
CS210 Intermediate Computing with Data Structures (Java)
Eclipse.
Lecture 1b- Introduction
The eclipse IDE IDE = “Integrated Development Environment”
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Working with Java.
CSC201: Computer Programming
Boolean Expressions and Conditionals (If Statements)
Logger, Assert and Invariants
Java on the LEGO Mindstorms EV3
Introduction to Computer Science / Procedural – 67130
Eclipse Navigation & Usage.
Testing and Debugging.
Java programming lecture one
Introduction to C Topics Compilation Using the gcc Compiler
User-Defined Functions
Statements, Comments & Simple Arithmetic
Phil Tayco Slide version 1.0 Created Oct 2, 2017
How to Run a Java Program
Introduction to Algorithm Design
Chapter 1: Computer Systems
How to Run a Java Program
Focus of the Course Object-Oriented Software Development
Introduction to Java Brief history of Java Sample Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
(Computer fundamental Lab)
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Review of Previous Lesson
How to Run a Java Program
Presentation transcript:

Introduction to Java Import Scanner class to use in our program Our program is a class, this is the header Our program consists of 1 method, called main Some variables (name, age, birth) and a constant Create an input object Prompt the user for their name and age Compute their age and output the result import java.util.Scanner; public class FirstProgram { public static void main(String[] args) String name; final int YEAR=2016; int age, birth; Scanner in = new Scanner(System.in); System.out.print("Enter your name: "); name = in.next(); System.out.println("Hello " + name + ", how old are you?"); age = in.nextInt(); birth = YEAR - age; System.out.println("You were born in either " + birth + " or " + (birth-1)); }

What is a Program? A program is a list of instructions written in a programming language that instructs the computer precisely what to do and is written so that it is unambiguous and grammatically correct Computers run programs they don’t actually understand the programs, they just execute them Computers are not capable of running programs in any language other than their native machine language Java is not that language, therefore we have to translate every program we write into machine language we use either a compiler or an interpreter to do this for us programs that have syntax errors will not compile programs that have logical errors will run but not necessarily correctly programs may also abnormally terminate with run-time errors In this course, we learn Java because it is popular, powerful and somewhat easy to learn – also, being an OOPL, we can learn about OOP

A Simple Java Program: Hello World // a simple introductory program public class HelloWorld { public static void main(String[] args) { System.out.println(“Hello World!”); } Let’s explore the above program The first line, starting with //, is a comment – it does nothing The second line is our class header, always of the form public class name later in the semester we will also learn about using the word private The third line is the header of the main method we will have more methods in later programs but not for now, and later in the semester we will learn what all the other stuff is in that header (void, static, String[] args) The fourth line is our only executable statement, it outputs “Hello World!” the { } are delimiters to indicate the start and end of blocks the indentation and placing } on separate lines promotes readability but is not necessary

Anatomy of a Java Program Import statements precede any class definition we must import classes that we are using in our program unless those classes are built-in classes (such as System and Math) Class header – starts a new class Methods (early in the semester, we will only write 1 method, main) methods consist of a header, { } and statements in { } Inside { } we can have variable and constant declarations executable statements input statements output statements assignment statements calls to other methods logic statements (selection statements, loops) comments (comments can go anywhere we like in a program)

Executable Statements These statement use reserved words and reserved punctuation marks we must spell the reserved words correctly or else we get syntax errors spelling includes using the right upper or lower case Some reserved words: public, private, static, int, void, String, final, new, while, for One type of executable statement is the assignment statement syntax: variable = expression; the expression can be a value, another variable, a method call, a mathematical expression, or some combination Most executable statements end with a ; learning when and where to use ; is a challenge but essential Some punctuation marks we need: = + - * / % ; . ( ) { } [ ] , / \ ! < > _ see the next slide

Common Java Punctuation Usage

Block Styles We’ve seen two ways to indicate { } Which should we use?   Blocks are chunks of executable code We place blocks inside methods and inside certain types of executable statements like for loops The code in a block is placed inside { } We’ve seen two ways to indicate { } Which should we use? it’s a matter of style, I prefer the one at the top of this slide but use the second one when I need to save space such as in these slides a lot of programmers use the version on the right because its quicker and saves space if you are printing out your code either is fine for this class

Why is Programming Challenging? It is hard to express your ideas of how to do something in specific code writing proper logic is hard you might write code but it will not necessarily work correct Your code will often have errors in it syntax errors – errors with the syntax of the language forgetting a ; or misspelling a reserved word or using the wrong data type run-time errors – errors that arise while the program is running asking the computer to do something it cannot do run-time error usually cause the program to terminate, in Java there is a mechanism to keep the program running known as exception handling (we see this in CSC 360) logical errors – errors with the logic of the program the program may run but might or will yield wrong answers e.g., using < instead of > or * instead of - as simple examples

Locate the Syntax Errors public class AnError { public static void main(String[] args) { System.out.println(“Hello world); } public class AnError2 { public static void main(String[] foo) System.out.println(“Hello world”); private class AnError3 { System.out.println(“Hello world”)

Java and Eclipse Java is a programming language it comes in two forms JDK – the development kit, needed to write and compile code, we are using JDK 1.8 JRE – the run-time environment, needed to run compiled code such as in a web browser your home computer almost certainly has the JRE but you will want to install the JDK if you do not have it, go to oracle.com Oracle purchased Sun Microsystems who created Java, Oracle maintains it http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html there are many IDEs (development environments) available, we will use Eclipse because it is free and runs in Windows, Mac and Linux go to http://www.eclipse.org/downloads/packages/eclipse-ide-java-and-dsl-developers/neonr and select the appropriate download link if you do not have the Java JDK, install it first when you install Eclipse, it will guess where your JDK is, and if it can’t find it you will have to specify it yourself

Using Eclipse Start Eclipse Select the directory for your workspace you can set up a permanent workspace so that you aren’t asked this again Select File  New  Java Project specify a unique project Name and use all of the defaults, click on Finish Make sure your new project is selected in the Package Explorer and select File  New  Class specify a Name for the class this Name will also be used in the filename as Name.java it may but does not have to be the same Name as the project name Eclipse advises you to use a different name although that is not necessary select the checkbox for public static void main(String[] args) later in the semester, we will use a different “method stub” leave the rest of the defaults as is, click Finish

Continued Edit your program To run your program, click on the Run button or select Run from the Run menu by default, your program will be compiled before it is run if you want to separately compile your program from running it, turn off “Build Automatically” (under the Project menu) if you do so, you will have to separately compile your program before running it by selecting Build Project from the Project menu the reason to separate the two steps is to avoid problems when you make changes to the program and run it only to see the old version run because the new version did not compile Most syntax errors will be caught before you compile and highlighted with an X on the line move your mouse cursor over the X and it will display an error message Output will appear at the bottom under the Console tab