CS107 Introduction to Computer Science Java Basics.

Slides:



Advertisements
Similar presentations
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Advertisements

Introduction to Programming G51PRG University of Nottingham Revision 1
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine.
JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. GCOC – A.P. Computer Science A College Board Computer Science A Topics Covered Program Design - Read and understand.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
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.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Hello, world! Dissect HelloWorld.java Compile it Run it.
CS107 Introduction to Computer Science Java Basics.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
High-Level Programming Languages: C++
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Spring 09- ICE0124 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 2.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
Introduction to Java Java Translation Program Structure
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition D.S. Malik D.S. Malik.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
Programming in java Lecture-2 Java Introduction. Platform Computing platform include hardware and software framework, this combination allows software.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
Overview of Java CSCI 392 Day One. Running C code vs Java code C Source Code C Compiler Object File (machine code) Library Files Linker Executable File.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Structured Programming (4 Credits) HNDIT Week 2 – Learning Outcomes Design an algorithmic solution for simple problem such as computation of a factorial,
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
Java FilesOops - Mistake Java lingoSyntax
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Unit 1 Review By: Mr. Jacobs.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Computer Programming Your First Java Program: HelloWorld.java.
Basic concepts of C++ Presented by Prof. Satyajit De
Dept of Computer Science University of Maryland College Park
GC101 Introduction to computer and program
Chapter 3 GC 101 Java Fundamentals.
Introduction to.
Data types and variables
Intro to Java.
Introduction to Programming in Java
Introduction to C++ Programming
An Introduction to Java – Part I, language basics
Introduction CSC 111.
Java Intro.
Lecture 13 Introduction to High-Level Programming (S&G, §§7.1–7.6)
Unit 3: Variables in Java
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
F II 2. Simple Java Programs Objectives
Computer Programming-1 CSC 111
Presentation transcript:

CS107 Introduction to Computer Science Java Basics

Java Java was developed in the early 90s by Sun Microsystems Java is a high-level language Java programs are portable across platforms –Each program is translated into Java bytecode –Each machine has a Java Virtual Machine (JVM) which knows how to execute Java bytecode Java is object-oriented –We will not use objects in this class

A Java program /* Here you describe what your program does. Laura Toma Csci107 */ public class CLASS-NAME { public static void main (String args[]) { // your program goes here } // end of main } // end of class

Compiling and Running In order to run a Java program: First you compile it –that is, you run a program called compiler that checks whether the program follows the Java syntax –if it finds errors, it lists them –If there are no errors, it translates the program into Java bytecode –Example: assume you created a program called Hello.java prompt>javac Hello.java –If successful, this creates a file Hello.class which contains the translation (Java bytecode) of Hello.java Then you execute it –That is, you call the Java Virtual Machine to interpret and execute the Java bytecode of your program –Example: prompt>java Hello

The infamous Hello world program When learning a new language, the first program people usually write is one that salutes the world :). Here is the Hello world program in Java. /* This program prints out “hello world!” and terminates. */ public class Hello { public static void main (String args[]) { System.out.println(“Hello world!”); } // end of main } // end of class

Notes Comments –what follows after // on the same line is considered comment –Or, what is in between /* this is a comment */ Indentation –is for the convenience of the reader; compiler ignores all spaces and new lines ; the delimiter for the compiler is the semicolon All statements ended by semicolon Lower vs. upper case matters!! –Void is different than void –Main is different that main

Variable declaration type variable-name; Meaning: variable will be a variable of type Where type can be: –int//integer –double//real number –char//character Example: int a, b, c; double x; int sum; char my-character;

Input /* this line should appear once in your program; basically it declares a variable r which knows how read input from the user */ ReadStream r = new ReadStream(); /* Then you can use r.readInt(), r.readDouble() or r.readChar() to read integers, decimal and character value from the user. */ int a; a= r.readInt(); r.readLine(); /* Meaning: read an integer from the user and store it into the variable called a */

Input Reading decimal numbers double a; a= r.readDouble(); r.readLine(); /* Meaning: read a decimal value from the user and store it into the variable called a*/ Reading characters char a; c= r.readChar(); r.readLine(); /* Meaning: read a character from the user and store it into the variable called a*/ Note that every read must be followed bythe r.readLine() instruction. This discards the rest of the line entered by the user.

Output System.out.println(variable-name); prints the value of variable to the user System.out.println(“any message “); prints the message within quotes to the user Note: System.out.println() always prints on a new line. System.out.println(“hello” + “world” + a + “plus“ + b);

If statements if (condition) { S1; } else { S2; } S3; condition S1 S2 S3 TrueFalse

Boolean conditions..are built using Comparison operators == equal != not equal < less than > greater than <= less than or equal >= greater than or equal Boolean operators && and || or ! not

Examples Assume we declared the following variables: int a = 2, b=5, c=10; Here are some examples of boolean conditions we can use: if (a == b) … if (a != b) … if (a <= b+c) … If ((a <= b) && (b <= c)) … if !((a < b) && (b<c)) …

While statements while (condition) { S1; } S2; condition S1 S2 TrueFalse

Example /* This program reads 100 numbers from the user and outputs their sum */ public class ComputeSum { public static void main (String args[]) { ReadStream r = new ReadStream(); int i, sum, x; sum=0; i=1; while (i <= 100) { x = r.readInt(); r.readLine(); sum = sum + x; i = i+1; } System.out.println(“sum is “ + sum); System.out.println(“Goodbye”); } //end of main }//end of class

Class exercise Write a program that asks the user –Do you want to use this program? (y/n) If the user says ‘y’ then the program terminates If the user says ‘n’ then the program asks –Are you really sure you do not want to use this program? (y/n) –If the user says ‘n’ it terminates, otherwise it prints again the message –Are you really really sure you do not want to use this program? (y/n) –And so on, every time adding one more “really”.