Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Computer Programming w/ Eng. Applications
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Some basic I/O.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
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;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Help session - C++ Arranged by : IEEE – NIIT Student Chapter.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
2440: 211 Interactive Web Programming Expressions & Operators.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Chapter 2: Java Fundamentals
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
CSC 107 – Programming For Science. The Week’s Goal.
Warm-Up: Monday, March 24 List as many commands in Java as you can remember (at least 10)
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
CSC 107 – Programming For Science. Announcements.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
By Mr. Muhammad Pervez Akhtar
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
(Dreaded) Quiz 2 Next Monday.
Week 3 - Monday.  What did we talk about last time?  Using Scanner to get input  Basic math operations  Lab 2.
CompSci 230 S Programming Techniques
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CSC111 Quick Revision.
Yanal Alahmad Java Workshop Yanal Alahmad
Introduction to C++ October 2, 2017.
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
IDENTIFIERS CSC 111.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 2 Programming Basics.
Introduction to Primitives
Input, Variables, and Mathematical Expressions
CSE 1321 Modules 1-5 Review Spring 2019
Presentation transcript:

Week #2 Java Programming

Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check “Show Line Numbers”

Simple Output For printing words (or Strings) onto the screen, we use this command: System.out.println(“Anything you want printed here”); If we want to print something that contains a variable, we would use this command: String mySentence = “Hey there.”; System.out.println(mySentence);

Simple Output String commands: \”This represents a quotation mark \’This represents an apostrophe \nThis represents a new line \\This represents a backslash \tThis represents a tab \sThis represents a space

Simple Output Another way to print out text onto your screen: This is used when you have a lot of variables to be included in the print statement, example: String name = “Vincent Tsang”; int age = 60; String school = “Binghamton University”; System.out.format(“My name is %s, I am %s years old, I go to %s.”, name, age, school);

Simple Output Printing out variables using the System.out.println(“”); format: String name = “Vincent Tsang”; int age = 60; String school = “Binghamton University”; System.out.println(“My name is ” + name + “. I am “ + age + “years old, I go to “ + school + “.”);

If/Else Statements What is an if/else statement? Basically, in programming, we will often have different ways to approach things depending on the values given to it. We tell the program that “If something occurs, do this. If something else occurs, do something else. Else do something else” T his will be clearer when we write out the code

If/Else Statements The command for the if/else statement is in this format: if(comparison) { Something happens here; } else { Something else happens here; }

If/Else Statements You are allowed to have more than one if statement: if(comparison) { Something happens here; } if(comparison2) { Something else happens here; } else { Something else happens here; }

If/Else Statements Nested If/Else Statements: if(comparison) { if(comparison2) {// Basically the same as && symbol Something happens here; } } else { Something else happens here; }

If/Else Statements Logical Operators || represents “or” This can be found above your enter key (it’s located with the backslash) && represents “and” This can be found with the 7 key Switch Statements This is used when you have a lot of things to compare and do different type of commands This is to save time so that you don’t have to type out a billion if statements

If/Else Statements In a switch statement, we need a variable to compare its value with: Example: int month;// This is our integer variable switch(month) { case 1: System.out.println(“January”); break; // We will always need to break out of the statement case 2: System.out.println(“February”); break; } Do Exercise 1 (ExerciseOne.java)

Scanners What is a scanner? Scanner will ask user for an input and store that input somewhere in the program to be used later Scanners are not imported into java by default, you need to import this yourself by using this command at the top of your code: import java.util.Scanner;

Method Functions What is a method? A method is a collection of statements that performs an operation Steps to do something, like washing clothes you would first take your clothes out of the hamper, then put it into the washing machine, then turn on the washing machine, put in detergent etc. Function is another name for Methods Remember that function/method names are always capitalized except the first word. (Refer to Code Convention Format) Method contains two parts Method header Method body

Method Functions Method Header (Signature for the method) The heading that describes the method, example: public static void sayHi() {// Method Header Code goes here;// Method Body } Another Example (With return value): public static int getAge() { return 15; }

Method Functions with Parameters Same as before but this time we will be using parameters, we use parameters when we need to give the method outside information: Example: public static void main(String[] args) { int age = 20; print(age); // This is a method I created myself and not predefined by java } public static void print(int age) { System.out.println(age); }

Operators What are operators? Operators are mathematical symbols used to calculate numbers +, -, /, *, % +Addition -Subtraction /Division *Multiplication %Modulus

Increment / Decrement operators There are commands for you to increment or decrement numbers in a variable, for that we use ++ to increment and we use – to decrement, example: int counter = 0; counter++;// This will add 1 to counter counter--;// This will subtract 1 from the counter counter += 5;// This is the same as counter = counter + 5; counter -= 5;// This is the same as counter = counter – 5; Things to remember: ++, --, +=, -=

Math Library Functions Like the scanner, the math isn’t included by default, we need to import it: import java.lang.Math; Commands we can use from this library: AbsAbsolute Value PowPower CeilCeiling FloorFloor MaxMaximum MinMinimum SqrtSquare Root

Math Library Functions How to use these functions: Absolute value: int myInt = -5; int otherInt = Math.abs(myInt); System.out.println(otherInt);// Will print out 5 Power: System.out.println(Math.pow(otherInt, 2));

Math Library Functions Ceiling System.out.println(Math.ceil(25.4)); Floor System.out.println(Math.floor(37.9)); Maximum System.out.println(Math.max(otherInt, myInt)); Minimum System.out.println(Math.min(otherInt, 2));

Declaration Integers (Whole numbers) int myVariable = 5; Doubles (Numbers with decimal) double myVariable = 24.7; Characters (Letters) char myVariable = ‘a’; Boolean (True/false) boolean myVariable = false; boolean myVariable = true;

Declaration String (Words/Sentences) String myVariable = “Hello World!”; There are functions that are pre-made for strings which are very useful: substring(int n) –Returns everything after the n th position including the one on n th position on a string indexOf(String n) –Returns the index of the string n on a string isEmpty() –Returns true/false depending if the string is empty toLowerCase() –Returns a string that is all in lower case toUpperCase() –Returns a string that is all in upper case equals(String n) –Compares one string to another

Declaration String functions examples: String myVariable = “Hello”; Substring myVariable.substring(3);// Will return “lo”; myVariable.substring(3,4);// Will return “l”; myVariable.substring(0,2);// Will return ? indexOf myVariable.indexOf(“llo”);// Return 2 isEmpty myVariable.isEmpty();// Returns false

Declaration toLowerCase myVariable.toLowerCase();// Returns “hello” toUpperCase myVariable.toUpperCase();// Returns “HELLO” Equals myVariable.equals(“Hello”);// Returns true myVariable.equals(“hello”);// Returns false

Program 1 Create a simple calculator