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

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Begin Java Pepper. Objectives What is a program? Learn the basics of your programming tool: BlueJ Write a first Java program and see it run Make some.
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.
Chapter 2: Your First Program! Hello World: Let’s Program  All programs must have the extension.java  Our first program will be named: HelloWorld.java.
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.
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,
01 Introduction1June Introduction CE : Fundamental Programming Techniques.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
An intro to programming The basic nitty-gritty that’ll make you scratch your head and say, “This is programming?”
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Slide 1 of 40. Lecture A The Java Programming Language Invented 1995 by James Gosling at Sun Microsystems. Based on previous languages: C, C++, Objective-C,
Introduction to Java.
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;
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
1 Building Java Programs Chapter 1: Introduction to Java Programming These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may.
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++
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
CS107 Introduction to Computer Science Java Basics.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Chapter 1 CSIS-120: Java Intro. What is Programming?  A: It is what makes computer so useful.  The flexibility of a computer is amazing  Write a term.
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.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
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.
1 WELCOME TO CIS 1068! Instructor: Alexander Yates.
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.
Decisions, Decisions, Decisions Conditional Statements In Java.
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
CS 106 Introduction to Computer Science I 01 / 22 / 2007 Instructor: Michael Eckmann.
© 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.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
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.
Introduction CMSC 202 Fall Instructors Mr. Ryan Bergeron – Lecture Section 01 Tues/Thu 1:00 – 2:15 am, Sondheim 111 – Lecture Section 04 Tues/Thu.
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
Introduction CSC 111.
Java Intro.
Introduction to Java Brief history of Java Sample Java Program
Lecture 13 Introduction to High-Level Programming (S&G, §§7.1–7.6)
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 instructions end by semicolon Lower vs. upper case matters!! –Void is different than void –Main is different that main

Writing to 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 System.out.println(“hello” + “world” + a + “plus“ + b); assuming the value of a is 3 and of b is 7, it prints helloworld3plus7 Note: System.out.println() always prints on a new line.

Example /* This program illustrates the System.out.println command. */ public class Hello { public static void main (String args[]) { System.out.println(“This is my first Java program!”); System.out.print(“I like Java.”); System.out.print(“I think Java is cool.”); } // end of main } // end of class Exercise: change the print to println above. What is the difference?

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

Example /* Printing ages. */ public class MyFirstJavaProgram { public static void main (String args[]) { int myAge, myFriendAge; /* declare two integer variables */ myAge = 20; myFriendAge = myAge + 1; //one year older System.out.println(“Hello, I am “ +myAge + “years old, and my friend is “ + myFriendAge + “ years old”); System.out.println(“Goodbye”); } // end of main } // end of class

Reading from user (Input) /* This program shows an example of reading from user. */ public class MyFirstJavaProgram{ public static void main (String args[]) { /* 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 to read an integer value from the user. */ System.out.print(“Dear user, please enter an integer number:”); int a; a= r.readInt(); r.readLine(); /* Meaning: read an integer from the user and store it into the variable called a */ } // end of main } // end of class

If statements if (condition) { //instructions } else { //instructions } //instructions condition instructions TrueFalse instructions

Boolean conditions..are built using Comparison operators == equal != not equal < less than > greater than <= less than or equal >= greater than or equal Example: int x, y; //two variables //assume they have some values if (x <= y) { System.out.println(“x is smaller”); } else { System.out.println(“x is larger”); }

Class work Write a program that asks for two scores and prints out the loser and the winner. For example, it could look like this (or use your imagination!). Make sure you handle all cases (victory, loss, tie). Tell me Bowdoin score: 23 Tell me Bates score: 16 Congratulations, Bowdoin won!! Goodbye.

Class work Write a program to compute the miles-per-gallon gas consumption of a car during a trip. Your program should ask the user for start and end mileage, and the amount of gas consumed. Then it should print out the result, and an appropriate message, depending whether the consumption is lower, equal to, or greater than 25 miles-per-gallon. Enter the start mileage of your trip: Enter the end mileage of your trip: Enter the number of gallons you filled the tank: 20.7 Your car gives miles-per-gallon. Congratulations!! Goodbye.