Building Java Programs. Introduction to Java Programming

Size: px
Start display at page:

Download "Building Java Programs. Introduction to Java Programming"

Transcription

1 Building Java Programs Introduction to Java Programming 1

2 What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed for easy Web/Internet applications Widespread acceptance

3 Java Features (1) Simple fixes some clumsy features of C++ no pointers automatic garbage collection rich pre-defined class library Object oriented focus on the data (objects) and methods manipulating the data all functions are associated with objects almost all datatypes are objects (files, strings, etc.) potentially better code organization and reuse

4 Java Features (2) Interpreted java compiler generate byte-codes, not native machine code the compiled byte-codes are platform-independent java bytecodes are translated on the fly to machine readable instructions in runtime (Java Virtual Machine) Portable same application runs on all platforms the sizes of the primitive data types are always the same the libraries define portable interfaces

5 Java Features (3) Reliable extensive compile-time and runtime error checking no pointers but real arrays. Memory corruptions or unauthorized memory accesses are impossible automatic garbage collection tracks objects usage over time Secure usage in networked environments requires more security memory allocation model is a major defense access restrictions are forced (private, public)

6 Java Features (4) Multithreaded multiple concurrent threads of executions can run simultaneously utilizes a sophisticated set of synchronization primitives (based on monitors and condition variables paradigm) to achieve this Dynamic java is designed to adapt to evolving environment libraries can freely add new methods and instance variables without any effect on their clients interfaces promote flexibility and reusability in code by specifying a set of methods an object can perform, but leaves open how these methods should be implemented can check the class type in runtime

7 Language basics (1) Data types 8 primitive types: boolean, byte, short, int, long, float, double, char Class types, either provided by Java, or made by programmers String, Integer, Array, Frame, Object, Person, Animal, Array types Variables datatype identifier [ = Expression]: Example variable declarations and initializations: int x; x=5; boolean b = true; Frame win = new Frame(); String x = how are you? ; int[] intarray; intarray = new int[2]; intarray[0] = 12; intarray[1] = 6; Person parray = new Person[10];

8 Language basics (2) Flow of control if, if-else, if-else if switch for, while, do-while break continue

9 Data Types Variables & Initializations String for strings of letters e.g. String LastName = Kim ; char for characters e.g. char alphabet = A ; byte, short, int, and long for integer numbers e.g. int number = 3; float & double for real numbers e.g. double pi = boolean for logical e.g. boolean check = true;

10 Expressions and Operators Expressions: ( ), *,/, +, - Operators: && for AND for OR! for NOT == for equal!= for NOT equal

11 Selection Statements if statement ex) if (A>B) { System.out.println( A>B ); if else statement ex) if (A>B) { System.out.println( A>B ); else { System.out.println( A<=B ); switch Statement ex) switch (A) { case 1: { System.out.println( A=1 ); break; case 2: { System.out.println( A=2 ); break; default: { System.out.println( A Undefined );

12 Repetitions(while, do while, and for) An example adding all numbers between 1 to 100 Using While: // Variable Declaration & Initialization int sum = 0; int count = 0; // Processing while (count <= 1000) { sum = sum + count; count = count +1; //same as count++ // Output System.out.println( Sum: + sum);

13 Repetitions(while, do while, and for) Using do-while: // Variable Declaration & Initialization int sum =0; int count =0; // Processing do { sum = sum + count; count = count +1; //same as count++ while (count <=1000); Using for: // Variable Declaration & Initialization int sum =0; // Processing for (int count =0; count <= 100; count++) { sum = sum +count;

14 Introducing Methods A method is a collection of statements that are grouped together to perform an operation. Method Structure returnvaluetype modifier methodname parameter list public static double readdouble() { double d = 0.0; try { String str = df.readline(); st = new StringTokenizer (str); d = new Double (st.nexttoken()).doublevalue(); catch (IOException ex) { System.out.println(ex); return d;

15 Declaring Methods /** * This method returns the maximum of two numbers num1 the first number num2 the second number either num1 or num2 */ int max(int num1, int num2) { if (num1 > num2) { else { return num1; return num2;

16 Passing Parameters /** * This method prints a message a specified number * of times. message the message to be printed n the number of times the message is to be * printed */ void nprintln(string message, int n) { for (int i=0; i<n; i++) { System.out.println(message);

17 The main method One of these methods is required for every program you write. If your program does not have one, then you cannot run it. The main method has a very specific syntax: public static void main(string[] args) { // Your code goes here! For example: public static void main(string[] args) { System.out.println( Hello World! );

18 An Example Program class MyProgram { /** * This method prints a message a specified number * of times. message the message to be printed n the number of times the message is to be * printed */ void nprintln(string message, int n) { for (int i=0; i<n; i++) { System.out.println(message); public static void main(string[] args) { nprintln( Hello World!, 10);

19 Object-Oriented Programming in Java Java programs are formed from a set of classes. A class is a container for code. Whenever you create a program, you write your code inside one or more classes. Some of the classes you will use in your programs are pre-written and are provided as part of the Java Application Programming Interfaces (APIs). Other classes you must write yourself.

20 Object-Oriented Programming in Java When we run a Java Program, we may create objects. Objects are instances of classes. Classes describes a general set of features. Objects specify concrete values for those features. For example: Let us start with a general description of a car: A Car has four wheels, an engine, a chassis, a colour, some doors The next two descriptions refer to particular cars. My car is brown and has a 2 litre engine, a Saab chassis, and five doors. Peter has a yellow car with a 1.4 litre engine, a VW Polo chassis, and three doors.

21 How does this relate to Java? The general description of a Car is like a class in Java a general description of what constitutes a car. Both my car and Peter s car are described in terms of the general description of a car (i.e. the colour, engine size, chassis type, number of doors). These particular cars are like objects in Java - a concrete instance of the general description of a Car. Note: The number of wheels is the same for all cars. We call things that are the same for all cars constants.

22 Data Attributes Methods, and Messages Classes contain two types of feature: Data attributes describe the information that a class contains (for example the colour of a car, the type of chassis, etc.) Methods describe operations that can be performed by instances (objects) of the class (for example, a Bank Account class may include methods to withdraw or deposit cash). To get an object to perform a method we send a message: This message must be sent to that object identifying the method to be performed.

23 Class Declaration class Circle { public double radius = 1.0; Data attribute /** * This method calculates the area of the circle. the area of the circle */ public double findarea() { return radius*radius* ; Method

24 Declaring Object Variables Syntax: ClassName objectname; Example: Circle mycircle;

25 Creating Objects Syntax: objectname = new ClassName(); Example: mycircle = new Circle();

26 Declaring/Creating Objects in a Single Step Syntax: ClassName objectname = new ClassName(); Example: Circle mycircle = new Circle();

27 Accessing Objects Referencing the object s data: Syntax: objectname.data Example: mycircle.radius Referencing the object s method (sending a message): Syntax: objectname.method( ) Example: mycircle.findarea()

28 Constructors Constructors are special methods that are called upon creation of an object. Their purpose is to initialise objects as they are created. Syntax: ClassName( ) { // Initialisation code goes here! Example: Circle(double r) { radius = r;

29 Constructors Example (cont): Circle() { radius = 1.0; We can specify which constructor to use when we create a new object. Example: mycircle = new Circle(5.0);

30 Modifiers By default, the class, variable, or data can be accessed by any class in the same package. public The class, data, or method is visible to any class in any package. private The data or methods can be accessed only by the declaring class.

31 An Example Program class TestCircle { public static void main(string[] args) { Circle mycircle = new Circle(); System.out.println("The area of the circle of radius " + mycircle.radius + " is " + mycircle.findarea()); class Circle { double radius = 1.0; /** * This method calculates the area of the circle. the area of the circle */ double findarea() { return radius*radius* ;

32 Basic Java programs with println statements 32

33 Computer programs program: A set of instructions that are to be carried out by a computer. program execution: The act of carrying out the instructions contained in a program. programming language: A systematic set of rules used to describe computations in a format that is editable by humans. 33

34 Languages A partial history of programming languages Some influential ones: FORTRAN science / engineering COBOL business data LISP logic and AI BASIC a simple language 34

35 Some modern languages procedural languages: programs are a series of commands Pascal (1970): designed for education C (1972): low-level operating systems and device drivers functional programming: functions map inputs to outputs Lisp (1958) / Scheme (1975), ML (1973), Haskell (1990) object-oriented languages: programs use interacting "objects" Smalltalk (1980): first major object-oriented language C++ (1985): "object-oriented" additions to C; successful in industrial programming (Windows is built in C++) Java (1995): Sun Microsystems' language designed for embedded systems, web applications, servers Runs on many platforms (Windows, Mac, Linux, cell phones...) The language taught in this textbook 35

36 A basic Java program public class Hello { public static void main(string[] args) { System.out.println("Hello, world!"); code or source code: The sequence of instructions in a program. The code in this program instructs the computer to display a message of Hello, world! on the screen. output: The messages printed to the user by a program. console: The text box onto which output is printed. Some editors pop up the console as an external "DOS" window, and others contain their own console window. 36

37 Compiling/running a program Before you run your programs, you must compile them. compiler: Translates a computer program written in one language into another language. Java Development Kit includes a Java compiler. The Java compiler converts your source code into a format named byte code that can be executed on many different kinds of computers. source code (Hello.java) compile byte code (Hello.class) execute output 37

38 Another Java program public class Hello2 { public static void main(string[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces"); System.out.println("four lines of output"); The code in this program instructs the computer to print four messages on the screen. 38

39 Structure of Java programs public class <name> { public static void main(string[] args) { <statement>; <statement>;... <statement>; Every executable Java program consists of a class that contains a method named main that contains the statements (commands) to be executed 39

40 Java terminology class: A module that can contain executable code. Every program you write will be a class. statement: An executable command to the computer. method: A named sequence of statements that can be executed together to perform a particular action. A special method named main signifies the code that should be executed when your program runs. Your program can have other methods in addition to main. (seen later) 40

41 Syntax syntax: The set of legal structures and commands that can be used in a particular programming language. some Java syntax: every basic Java statement ends with a semicolon ; The contents of a class or method occur between { and 41

42 Syntax and syntax errors syntax error or compiler error: A problem in the structure of a program that causes the compiler to fail. If you type your Java program incorrectly, you may violate Java's syntax and see a syntax error. 1 public class Hello { 2 pooblic static void main(string[] args) { 3 System.owt.println("Hello, world!")_ 4 5 compiler output: Hello.java:2: <identifier> expected pooblic static void main(string[] args) { ^ Hello.java:5: ';' expected ^ 2 errors 42

43 Fixing syntax errors Error messages do not always help us understand what is wrong: Hello.java:2: <identifier> expected pooblic static void main(string[] args) { ^ We'd have preferred a friendly message such as, "You misspelled 'public' " The compiler does tell us the line number on which it found the error... But it is not always the true source of the problem. 1 public class MissingSemicolon { 2 public static void main(string[] args) { 3 System.out.println("A rose by any other name") 4 System.out.println("would smell as sweet"); 5 6 MissingSemicolon.java:4: ';' expected System.out.println("would smell as sweet"); ^ 43

44 System.out.println System.out.println : A statement to instruct the computer to print a line of output on the console. pronounced "print-linn" sometimes called a "println statement" for short Two ways to use System.out.println : System.out.println("<Message>"); Prints the given message as a line of text on the console. System.out.println(); Prints a blank line on the console. 44

45 Strings and string literals string: A sequence of text characters that can be printed or manipulated in a program. sometimes also called a string literal strings in Java start and end with quotation mark " characters Examples: "hello" "This is a string" "This, too, is a string. It can be very long!" 45

46 Details about Strings A string may not span across multiple lines. "This is not a legal String." A string may not contain a " character. (The ' character is okay) "This is not a "legal" String either." "This is 'okay' though." A string can represent certain special characters by preceding them with a backslash \ (this is called an escape sequence). \t \n \" \\ tab character new line character quotation mark character backslash character Example: Output: System.out.println("\\hello\nhow\tare \"you\"?"); \hello how are "you"? 46

47 Questions What is the output of each of the following println statements? System.out.println("\ta\tb\tc"); System.out.println("\\\\"); System.out.println("'"); System.out.println("\"\"\""); System.out.println("C:\nin\the downward spiral"); Write a println statement to produce the following line of output: / \ // \\ /// \\\ 47

48 Questions What println statements will generate the following output? This program prints a quote from the Gettysburg Address. "Four score and seven years ago, our 'fore fathers' brought forth on this continent a new nation." What println statements will generate the following output? A "quoted" String is 'much' better if you learn the rules of "escape sequences." Also, "" represents an empty String. Don't forget to use \" instead of "! '' is not the same as " 48

49 Procedural decomposition using static methods 49

50 Algorithms algorithm: A list of steps for solving a problem. How does one bake sugar cookies? (what is the "bake sugar cookies" algorithm?) Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients. Set the oven for the appropriate temperature. Set the timer. Place the cookies into the oven. Allow the cookies to bake. Mix the ingredients for the frosting. Spread frosting and sprinkles onto the cookies. 50

51 Structured algorithms structured algorithm: An algorithm that is broken down into cohesive tasks. A structured algorithm for baking sugar cookies: 1. Make the cookie batter. Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients. 2. Bake the cookies. Set the oven for the appropriate temperature. Set the timer. Place the cookies into the oven. Allow the cookies to bake. 3. Add frosting and sprinkles. Mix the ingredients for the frosting. Spread frosting and sprinkles onto the cookies

52 Redundancy in algorithms How would we express the steps to bake a double batch of sugar cookies? Unstructured: Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients. Set the oven... Set the timer. Place the first batch of cookies into the oven. Allow the cookies to bake. Set the oven... Set the timer. Place the second batch of cookies into the oven. Allow the cookies to bake. Mix the ingredients for the frosting. Structured: 1. Make the cookie batter. 2a. Bake the first batch of cookies. 2b. Bake the second batch of cookies. 3. Add frosting and sprinkles. Observation: Hierarchical, thus easier to understand. Higher-level operations help eliminate redundancy. 52

53 A program with redundancy redundancy: Occurrence of the same sequence of commands multiple times in a program. public class TwoMessages { public static void main(string[] args) { System.out.println("Now this is the story all about how"); System.out.println("My life got flipped turned upside-down"); System.out.println(); System.out.println("Now this is the story all about how"); System.out.println("My life got flipped turned upside-down"); Program's output: Now this is the story all about how My life got flipped turned upside-down Now this is the story all about how My life got flipped turned upside-down We print the same messages twice in the program. 53

54 Static methods static method: A group of statements given a name. procedural decomposition: breaking a problem into methods using a static method requires two steps: 1.declare it (write down the recipe) write a group of statements and give it a name 2.call it (cook using the recipe) tell our program to execute the method static methods are useful for: denoting the structure of a larger program in smaller pieces eliminating redundancy through reuse 54

55 Declaring a static method The syntax for declaring a static method (writing down the recipe): public class <class name> { public static void <method name> () { <statement>; <statement>;... <statement>; Example: public static void printwarning() { System.out.println("This product is known to cause"); System.out.println("cancer in lab rats and humans."); 55

56 Calling a static method The syntax for calling a static method (cooking using the recipe). In another method such as main, write: <method name> (); Example: printwarning(); A benefit of static methods is that you can call the method multiple times. printwarning(); printwarning(); Resulting output: This product is known to cause cancer in lab rats and humans. This product is known to cause cancer in lab rats and humans. 56

57 A program w/ static method public class TwoMessages { public static void main(string[] args) { displaymessage(); System.out.println(); displaymessage(); public static void displaymessage() { System.out.println("Now this is the story all about how"); System.out.println("My life got flipped turned upside-down"); Program's output: Now this is the story all about how My life got flipped turned upside-down Now this is the story all about how My life got flipped turned upside-down 57

58 Methods calling methods One static method can call another: public class MethodsExample { public static void main(string[] args) { message1(); message2(); System.out.println("Done with main."); public static void message1() { System.out.println("This is message1."); public static void message2() { System.out.println("This is message2."); message1(); System.out.println("Done with message2."); Program's output: This is message1. This is message2. This is message1. Done with message2. Done with main. 58

59 Control flow of methods When a method is called: the execution "jumps" into that method, executes all of its statements, and then "jumps" back to the statement after the method call. public class MethodsExample { public static void main(string[] args) { public static void message1() { message1(); System.out.println("This is message1.");... message2(); public static void message2() { System.out.println("This is message2."); message1(); System.out.println("Done with main."); System.out.println("Done with message2."); public static void message1() { System.out.println("This is message1."); 59

60 Static method problems Write a program that prints the following output to the console. Use static methods as appropriate. I do not like my spam, I do not like them, Sam I am! I do not like them on my screen, I do not like them to be seen. I do not like my spam, I do not like them, Sam I am! Write a program that prints the following output to the console. Use static methods as appropriate. Lollipop, lollipop Oh, lolli lolli lolli Lollipop, lollipop Oh, lolli lolli lolli Call my baby lollipop 60

61 When to use static methods Place statements into a static method if: The statements are related to each other and form a part of the program's structure, or The statements are repeated in the program. You need not create static methods for: Individual statements only occurring once in the program. (A single println in a method does not improve the program.) Unrelated or weakly related statements. (Consider splitting the method into two smaller methods.) Only blank lines. (Blank println statements can go in the main method.) 61

62 Identifiers identifier: A name that we give to a piece of data or part of a program. Identifiers allow us to refer to that data later in the program. Identifiers give names to: classes methods variables, constants (seen in Ch. 2) Conventions for naming in Java: classes: capitalize each word (ClassName) methods: capitalize each word after the first (methodname) (variable names follow the same convention) constants: all caps, words separated by _ (CONSTANT_NAME) 62

63 Details about identifiers Java identifier names: first character must a letter or _ or $ following characters can be any of those or a number identifiers are case-sensitive (name is different from Name) Example Java identifiers: legal: susan second_place _myname TheCure ANSWER_IS_42 $variable illegal: me+u 49er question? side-swipe hi there ph.d jim's 2%milk suzy@yahoo.com 63

64 Keywords keyword: An identifier that you cannot use, because it already has a reserved meaning in the Java language. Complete list of Java keywords: abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized You may not use char or while or this for the name of a class or method; Java reserves those words to mean other things. You could use CHAR, While, or ThIs, because Java is case-sensitive. However, this could be confusing and is not recommended. 64

65 Comments comment: A note written in the source code by the programmer to make the code easier to understand. Comments are not executed when your program runs. Most Java editors show your comments with a special color. Comment, general syntax: /* <comment text; may span multiple lines> */ or, // <comment text, on one line> Examples: /* A comment goes here. */ /* It can even span multiple lines. */ // This is a one-line comment. 65

66 Using comments Where to place comments: at the top of each file (also called a "comment header"), naming the author and explaining what the program does at the start of every method, describing its behavior inside methods, to explain complex pieces of code (more useful later) Comments provide important documentation. Later programs will span hundreds or thousands of lines, split into many classes and methods. Comments provide a simple description of what each class, method, etc. is doing. When multiple programmers work together, comments 66

67 Comments example /* Suzy Student CS 101, Fall 2019 This program prints lyrics from my favorite song! */ public class MyFavoriteSong { /* Runs the overall program to print the song on the console. */ public static void main(string[] args) { sing(); // Separate the two verses with a blank line System.out.println(); sing(); // Displays the first verse of the theme song. public static void sing() { System.out.println("Now this is the story all about how"); System.out.println("My life got flipped turned upside-down"); 67

68 How to comment: methods Do not describe the syntax/statements in detail. Instead, provide a short English description of the observed behavior when the method is run. Example: // This method prints the lyrics to the first verse // of my favorite TV theme song. // Blank lines separate the parts of the verse. public static void verse1() { System.out.println("Now this is the story all about how"); System.out.println("My life got flipped turned upside-down"); System.out.println(); System.out.println("And I'd like to take a minute,"); System.out.println("just sit right there"); System.out.println("I'll tell you how I became the prince"); System.out.println("of a town called Bel-Air"); 68

69 Static methods question Write a program to print the following figures. Use static methods to capture structure and and eliminate redundancy. / \ / \ \ / \ / \ / \ / / \ / \ STOP \ / \ / / \ / \

70 Problem-solving methodology Some steps we can use to print complex figures: / \ First version of program (unstructured): / \ \ / \ / \ / \ / / \ / \ STOP \ / \ / / \ / \ Create an empty program with a skeletal header and main method. Copy the expected output into it, surrounding each line with System.out.println syntax. Run our first version and verify that it produces the correct output. 70

71 Program, version 1 // Suzy Student, CSE 142, Autumn 2047 // This program prints several assorted figures. // public class Figures1 { public static void main(string[] args) { System.out.println(" "); System.out.println(" / \\"); System.out.println("/ \\"); System.out.println("\\ /"); System.out.println(" \\ /"); System.out.println(); System.out.println("\\ /"); System.out.println(" \\ /"); System.out.println(" "); System.out.println(); System.out.println(" "); System.out.println(" / \\"); System.out.println("/ \\"); System.out.println(" STOP "); System.out.println("\\ /"); System.out.println(" \\ /"); System.out.println(); System.out.println(" "); System.out.println(" / \\"); System.out.println("/ \\"); System.out.println(" "); 71

72 Problem-solving 2 / \ / \ \ / \ / \ / \ / / \ / \ STOP \ / \ / Second version of program (structured with redundancy): Identify the structure of the output, and divide the main method into several static methods based on this structure. / \ / \

73 Problem-solving 2 answer / \ / \ \ / \ / \ / \ / / \ / \ STOP \ / \ / / \ / \ The structure of the output: initial "egg" figure second "teacup" figure third "stop sign" figure fourth "hat" figure This structure can be represented by methods: drawegg drawteacup drawstopsign drawhat 73

74 Program, version 2 // Suzy Student, CSE 142, Autumn 2047 // Prints several assorted figures, with methods for structure. // public class Figures2 { public static void main(string[] args) { drawegg(); drawteacup(); drawstopsign(); drawhat(); // Draws a figure that vaguely resembles an egg. public static void drawegg() { System.out.println(" "); System.out.println(" / \\"); System.out.println("/ \\"); System.out.println("\\ /"); System.out.println(" \\ /"); System.out.println(); // Draws a figure that vaguely resembles a teacup. public static void drawteacup() { System.out.println("\\ /"); System.out.println(" \\ /"); System.out.println(" "); System.out.println(); 74

75 Program, version 2, cont'd. // Draws a figure that vaguely resembles a stop sign. public static void drawstopsign() { System.out.println(" "); System.out.println(" / \\"); System.out.println("/ \\"); System.out.println(" STOP "); System.out.println("\\ /"); System.out.println(" \\ /"); System.out.println(); // Draws a figure that vaguely resembles a hat. public static void drawhat() { System.out.println(" "); System.out.println(" / \\"); System.out.println("/ \\"); System.out.println(" "); 75

76 Problem-solving 3 / \ / \ \ / \ / \ / \ / / \ / \ STOP \ / \ / Third version of program (structured without redundancy): Identify any redundancy in the output, and further divide the program into static methods to eliminate as much redundancy as possible. Add comments to the program to improve its readability. / \ / \

77 Problem-solving 3 answer / \ / \ \ / \ / \ / \ / / \ / \ STOP \ / \ / / \ / \ The redundancy in the output: top half of egg: reused on stop sign, hat bottom half of egg: reused on teacup, stop sign divider line: used on teacup, hat a single line, so making it a method is optional This redundancy can be fixed by methods: draweggtop draweggbottom drawline (optional) 77

78 Program, version 3 // Suzy Student, CSE 142, Autumn 2047 // Prints several figures, with methods for structure and redundancy. public class Figures3 { public static void main(string[] args) { drawegg(); drawteacup(); drawstopsign(); drawhat(); // draws redundant part that looks like the top of an egg public static void draweggtop() { System.out.println(" "); System.out.println(" / \\"); System.out.println("/ \\"); // draws redundant part that looks like the bottom of an egg public static void draweggbottom() { System.out.println("\\ /"); System.out.println(" \\ /"); // Draws a figure that vaguely resembles an egg. public static void drawegg() { draweggtop(); draweggbottom(); System.out.println(); 78

79 Program, version 3, cont'd. // Draws a figure that vaguely resembles a teacup. public static void drawteacup() { draweggbottom(); System.out.println(" "); System.out.println(); // Draws a figure that vaguely resembles a stop sign. public static void drawstopsign() { draweggtop(); System.out.println(" STOP "); draweggbottom(); System.out.println(); // Draws a figure that vaguely resembles a hat. public static void drawhat() { draweggtop(); System.out.println(" "); 79

80 Another example Write a program to print the following block letters spelling "banana". Use static methods to capture structure and and eliminate redundancy. BBBBB B B BBBBB B B BBBBB AAAA A A AAAAAA A A N N NNN N N NNN N N AAAA A A AAAAAA A A N N NNN N N NNN N N AAAA A A AAAAAA A A 80

Building Java Programs

Building Java Programs Building Java Programs Chapter 1: Introduction to Java Programming These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2007. They may not be rehosted, sold, or modified without expressed

More information

Procedural decomposition using static methods

Procedural decomposition using static methods Algorithms Procedural decomposition using static methods Recall: An algorithm is a list of steps for solving a problem. What is the algorithm to bake sugar cookies? 1 2 The Bake sugar cookies algorithm

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: 1.1-1.3 self-check: #1-14 exercises: #1-4 What is CSE? Computer Science Study of computation (information processing)

More information

Building Java Programs Chapter 1

Building Java Programs Chapter 1 Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson 2013. All rights reserved. What is computer science? Computer Science The study of theoretical foundations of information

More information

Building Java Programs. Introduction to Programming and Simple Java Programs

Building Java Programs. Introduction to Programming and Simple Java Programs Building Java Programs Introduction to Programming and Simple Java Programs 1 A simple Java program public class Hello { public static void main(string[] args) { System.out.println("Hello, world!"); code

More information

Building Java Programs. Chapter 1: Introduction to Java Programming

Building Java Programs. Chapter 1: Introduction to Java Programming Building Java Programs Chapter 1: Introduction to Java Programming Lecture outline Introduction Syllabus and policies What is computer science Programs and programming languages Basic Java programs Output

More information

Introduction to Computer Programming

Introduction to Computer Programming Introduction to Computer Programming Getting Started: An Introduction to Programming in Java What Is Programming? Computers cannot do all the wonderful things that we expect without instructions telling

More information

Lecture 1: Basic Java Syntax

Lecture 1: Basic Java Syntax Lecture 1: Basic Java Syntax Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved. Java Terminology class: (a) A module or program

More information

Entering the world of Javatar

Entering the world of Javatar Entering the world of Javatar Subset of the Supplement Lesson slides from: Building Java Programs by Stuart Reges and Marty Stepp (http://www.buildingjavaprograms.com/ ) Compiling/running programs 1. Write

More information

Static Methods & Decomposition

Static Methods & Decomposition Static Methods & Decomposition Subset of the Supplement Lesson slides from: Building Java Programs, Chapter 1 by Stuart Reges and Marty Stepp (http://www.buildingjavaprograms.com/ ) Warm Up Questions What

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site I hope to have a course web site up on Blackboard soon However, I am using the following site all semester to allow

More information

Building Java Programs Chapter 1

Building Java Programs Chapter 1 Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson 2013. All rights reserved. The CS job market 160,000 140,000 120,000 100,000 80,000 60,000 PhD Master's Bachelor's

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 1 Lecture 2: Static Methods reading: 1.4-1.5 (Slides adapted from Stuart Reges, Hélène Martin, and Marty Stepp) 2 Recall: structure, syntax class: a program public class

More information

Building Java Programs

Building Java Programs Comments Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading: 1.4-1.5 comment: A note written in source code by the programmer to describe or clarify the code. Comments are not executed

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading: 1.4-1.5 2 Recall: structure, syntax class: a program public class name { public static void main(string[] args) { statement; statement;...

More information

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs Welcome to CSE 142! Zorah Fung University of Washington, Spring 2015 Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs reading: 1.1-1.3 1 What is computer science? computers?

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming (Spring 2012) Lecture #6: Static Methods and Decomposition Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112

More information

Recap: Structure of a Java program CS 112 Introduction to Programming. A Foundation for Programming Why Introduce Static Methods?

Recap: Structure of a Java program CS 112 Introduction to Programming. A Foundation for Programming Why Introduce Static Methods? Recap: Structure of a Java program CS 112 Introduction to Programming A class: - has a name, defined in a file with same name Convention we follow: capitalize each English word - starts with {, and ends

More information

Topic 3 static Methods and Structured Programming

Topic 3 static Methods and Structured Programming Topic 3 static Methods and Structured Programming "The cleaner and nicer the program, the faster it's going to run. And if it doesn't, it'll be easy to make it fast." -Joshua Bloch Based on slides by Marty

More information

CSE 142, Summer 2015

CSE 142, Summer 2015 CSE 142, Summer 2015 Lecture 2: Static Methods Expressions reading: 1.4 2.1 The Mechanical Turk 2 Escape Characters System.out.println( ab\ \\\\\\/\td ); Output: ab \\\/ d 3 Algorithms algorithm: A list

More information

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018 Welcome to CSE 142! Whitaker Brand University of Washington, Winter 2018 1 What is computer science? computers? science? programming? late lonely nights in front of the computer? ALGORITHMIC THINKING al

More information

CSc 110, Autumn Lecture 2: Functions

CSc 110, Autumn Lecture 2: Functions CSc 110, Autumn 2017 Lecture 2: Functions Escape sequences escape sequence: A special sequence of characters used to represent certain special characters in a string. \t tab character \n new line character

More information

CSE 142, Summer 2014

CSE 142, Summer 2014 CSE 142, Summer 2014 Lecture 2: Static Methods Expressions reading: 1.4 2.1 Algorithms algorithm: A list of steps for solving a problem. Example algorithm: "Bake sugar cookies" Mix the dry ingredients.

More information

Lecture 1: Overview of Java

Lecture 1: Overview of Java Lecture 1: Overview of Java What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed for easy Web/Internet applications Widespread

More information

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education CSE 142 Lecture 1 Course Introduction; Basic Java Welcome Today: Course mechanics A little about computer science & engineering (CSE) And how this course relates Java programs that print text 2 Handouts

More information

Getting Started With Java

Getting Started With Java Getting Started With Java Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar, Gyan Khand-3, Indirapuram, Ghaziabad Website: www.sisoft.in Email:info@sisoft.in Phone: +91-9999-283-283 Java - Overview

More information

CSc 110, Spring Lecture 2: Functions. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Spring Lecture 2: Functions. Adapted from slides by Marty Stepp and Stuart Reges CSc 110, Spring 2017 Lecture 2: Functions Adapted from slides by Marty Stepp and Stuart Reges 1 Review From last lecture: print, strings, escape sequences. What is the output of the following statement?

More information

clicker question Comments Using comments Topic 3 static Methods and Structured Programming

clicker question Comments Using comments Topic 3 static Methods and Structured Programming Topic 3 static Methods and Structured Programming "The cleaner and nicer the program, the faster it's going to run. And if it doesn't, it'll be easy to make it fast." -Joshua Bloch clicker question What

More information

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords Introduction to Java Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords Program Errors Syntax Runtime Logic Procedural Decomposition Methods Flow of Control

More information

2 rd class Department of Programming. OOP with Java Programming

2 rd class Department of Programming. OOP with Java Programming 1. Structured Programming and Object-Oriented Programming During the 1970s and into the 80s, the primary software engineering methodology was structured programming. The structured programming approach

More information

CSE 201 Java Programming I. Smart Coding School website:

CSE 201 Java Programming I. Smart Coding School website: CSE 201 Java Programming I Smart Coding School website: http://www.smartcodingschool.com Definition: A named group of statements public static void methodname() { statements Usage: 1. denotes the structure

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Lecture #2: Java Program Structure Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu 2 1 Outline q Admin.

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors Outline Overview history and advantage how to: program, compile and execute 8 data types 3 types of errors Control statements Selection and repetition statements Classes and methods methods... 2 Oak A

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java Introduction Objectives An overview of object-oriented concepts. Programming and programming languages An introduction to Java 1-2 Problem Solving The purpose of writing a program is to solve a problem

More information

BASIC ELEMENTS OF A COMPUTER PROGRAM

BASIC ELEMENTS OF A COMPUTER PROGRAM BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators

More information

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing Introduction 1 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Focus of the Course Object-Oriented Software Development

More information

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Writing a Simple Java Program Intro to Variables Readings Your textbook is Big Java (3rd Ed). This Week s Reading: Ch 2.1-2.5, Ch

More information

COMP 202 Java in one week

COMP 202 Java in one week COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of

More information

CompSci 125 Lecture 02

CompSci 125 Lecture 02 Assignments CompSci 125 Lecture 02 Java and Java Programming with Eclipse! Homework:! http://coen.boisestate.edu/jconrad/compsci-125-homework! hw1 due Jan 28 (MW), 29 (TuTh)! Programming:! http://coen.boisestate.edu/jconrad/cs125-programming-assignments!

More information

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic BIT 3383 Java Programming Sem 1 Session 2011/12 Chapter 2 JAVA basic Objective: After this lesson, you should be able to: declare, initialize and use variables according to Java programming language guidelines

More information

Introduction to Java

Introduction to Java Introduction to Java Module 1: Getting started, Java Basics 22/01/2010 Prepared by Chris Panayiotou for EPL 233 1 Lab Objectives o Objective: Learn how to write, compile and execute HelloWorld.java Learn

More information

Games Course, summer Introduction to Java. Frédéric Haziza

Games Course, summer Introduction to Java. Frédéric Haziza Games Course, summer 2005 Introduction to Java Frédéric Haziza (daz@it.uu.se) Summer 2005 1 Outline Where to get Java Compilation Notions of Type First Program Java Syntax Scope Class example Classpath

More information

Java Programming Language Mr.Rungrote Phonkam

Java Programming Language Mr.Rungrote Phonkam 3 Java Programming Language Mr.Rungrote Phonkam rungrote@it.kmitl.ac.th Contents 1. Identify 2. Method Member 3. Literals 4. Data Type 6. Variable 1. Identify ก ก Class, Data, Method, Variable, Label,

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Overview of Source Code Components Comments Library declaration Classes Functions Variables Comments Can

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

An overview of Java, Data types and variables

An overview of Java, Data types and variables An overview of Java, Data types and variables Lecture 2 from (UNIT IV) Prepared by Mrs. K.M. Sanghavi 1 2 Hello World // HelloWorld.java: Hello World program import java.lang.*; class HelloWorld { public

More information

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public

More information

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

UEE1302 (1102) F10: Introduction to Computers and Programming

UEE1302 (1102) F10: Introduction to Computers and Programming Computational Intelligence on Automation Lab @ NCTU Learning Objectives UEE1302 (1102) F10: Introduction to Computers and Programming Programming Lecture 00 Programming by Example Introduction to C++ Origins,

More information

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. Today! Build HelloWorld yourself in BlueJ and Eclipse. Look at all the Java keywords. Primitive Types. HelloWorld in BlueJ 1. Find BlueJ in the start menu, but start the Select VM program instead (you

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing Java Programming MSc Induction Tutorials 2011 Stefan Stafrace PhD Student Department of Computing s.stafrace@surrey.ac.uk 1 Tutorial Objectives This is an example based tutorial for students who want to

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science? Outline CIS 110: Introduction to Computer Programming Lecture 1 An introduction of an introduction ( 1.1 1.3)* 1. What is computer science and computer programming? 2. Introductions and logistics 3. The

More information

For the course, we will be using JCreator as the IDE (Integrated Development Environment).

For the course, we will be using JCreator as the IDE (Integrated Development Environment). For the course, we will be using JCreator as the IDE (Integrated Development Environment). We strongly advise that you install these to your own computers. If you do not have your own computer, the computer

More information

: Primitive data types Variables Operators if, if-else do-while, while, for. // // First Java Program. public class Hello {

: Primitive data types Variables Operators if, if-else do-while, while, for. // // First Java Program. public class Hello { 2110211 : 2110211 Primitive data types Variables Operators if, if-else do-while, while, for 2110211 7/11/2002 2 // // First Java Program public class Hello { // // main method public static void main(string[]

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics WIT COMP1000 Java Basics Java Origins Java was developed by James Gosling at Sun Microsystems in the early 1990s It was derived largely from the C++ programming language with several enhancements Java

More information

The Java Language Rules And Tools 3

The Java Language Rules And Tools 3 The Java Language Rules And Tools 3 Course Map This module presents the language and syntax rules of the Java programming language. You will learn more about the structure of the Java program, how to insert

More information

Chapter 2. Elementary Programming

Chapter 2. Elementary Programming Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,

More information

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

CS Week 2. Jim Williams, PhD

CS Week 2. Jim Williams, PhD CS 200 - Week 2 Jim Williams, PhD Society of Women Engineers "Not just for women and not just for engineers; all students are welcome. We are especially looking for more computer science students!" - Emile

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Java Primitive Data Types; Arithmetic Expressions Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example Admin CS 112 Introduction to Programming q Programming assignment 2 to be posted tonight Java Primitive Data Types; Arithmetic Expressions Yang (Richard) Yang Computer Science Department Yale University

More information

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 2012 OBJECT ORIENTED PROGRAMMING Time Allotted : 3 Hours Full Marks : 70 The figures in the margin indicate full marks. Candidates are required to give their answers

More information

First Java Program - Output to the Screen

First Java Program - Output to the Screen First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the

More information

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا Tutorial 1 CSC 201 Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا م- م- م- م- م- Chapter 1 1. What is Java? 2. Why Learn Java? a. Java Is Platform Independent b. Java is Easy to learn 3. Programming

More information

CS11 Java. Fall Lecture 1

CS11 Java. Fall Lecture 1 CS11 Java Fall 2006-2007 Lecture 1 Welcome! 8 Lectures Slides posted on CS11 website http://www.cs.caltech.edu/courses/cs11 7-8 Lab Assignments Made available on Mondays Due one week later Monday, 12 noon

More information

Fundamentals of Programming. By Budditha Hettige

Fundamentals of Programming. By Budditha Hettige Fundamentals of Programming By Budditha Hettige Overview Exercises (Previous Lesson) The JAVA Programming Languages Java Virtual Machine Characteristics What is a class? JAVA Standards JAVA Keywords How

More information

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos. Lecture 2: Variables and Operators AITI Nigeria Summer 2012 University of Lagos. Agenda Variables Types Naming Assignment Data Types Type casting Operators Declaring Variables in Java type name; Variables

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Names and Identifiers A program (that is, a class) needs a name public class SudokuSolver {... 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations,

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS Java language Part 1. Java fundamentals Yevhen Berkunskyi, NUoS eugeny.berkunsky@gmail.com http://www.berkut.mk.ua What Java is? Programming language Platform: Hardware Software OS: Windows, Linux, Solaris,

More information

ECE 122 Engineering Problem Solving with Java

ECE 122 Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Introduction to Programming for ECE Lecture 1 Course Overview Welcome! What is this class about? Java programming somewhat software somewhat Solving engineering

More information

Lesson 01 Introduction

Lesson 01 Introduction Lesson 01 Introduction MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Lecturer in Management & IT M.Sc. In IS (SLIIT), PGD in IS (SLIIT), BBA (Hons.) Spl. in IS (SEUSL), MCP Programs Computer

More information

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG 1 Notice Reading Assignment Chapter 1: Introduction to Java Programming Homework 1 It is due this coming Sunday

More information

Accelerating Information Technology Innovation

Accelerating Information Technology Innovation Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lesson 02 Variables and Operators Agenda Variables Types Naming Assignment Data Types Type casting Operators

More information

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Module 1: Introduction to Computers, Programs, and Java

Module 1: Introduction to Computers, Programs, and Java Module 1: Introduction to Computers, Programs, and Java Module 1: Introduction to Java page 1 Objectives To review Program Design and Problem-Solving Techniques To describe the relationship between Java

More information

Course Outline. Introduction to java

Course Outline. Introduction to java Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

More information

Java Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M.

Java Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M. Java Language Essentials Java is Case Sensitive All Keywords are lower case White space characters are ignored Spaces, tabs, new lines Java statements must end with a semicolon ; Compound statements use

More information

Java Bytecode (binary file)

Java Bytecode (binary file) Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.

More information

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process Entry Point of Execution: the main Method Elementary Programming EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG For now, all your programming exercises will

More information

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. Chapter 6 Methods 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 A Solution int sum = 0; for (int i = 1; i

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

JAVA Ch. 4. Variables and Constants Lawrenceville Press

JAVA Ch. 4. Variables and Constants Lawrenceville Press JAVA Ch. 4 Variables and Constants Slide 1 Slide 2 Warm up/introduction int A = 13; int B = 23; int C; C = A+B; System.out.print( The answer is +C); Slide 3 Declaring and using variables Slide 4 Declaring

More information

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

More information

Lab # 2. For today s lab:

Lab # 2. For today s lab: 1 ITI 1120 Lab # 2 Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot 1 For today s lab: Go the course webpage Follow the links to the lab notes for Lab 2. Save all the java programs you

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

13 th Windsor Regional Secondary School Computer Programming Competition

13 th Windsor Regional Secondary School Computer Programming Competition SCHOOL OF COMPUTER SCIENCE 13 th Windsor Regional Secondary School Computer Programming Competition Hosted by The School of Computer Science, University of Windsor WORKSHOP I [ Overview of the Java/Eclipse

More information

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS PAUL L. BAILEY Abstract. This documents amalgamates various descriptions found on the internet, mostly from Oracle or Wikipedia. Very little of this

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8 Epic Test Review 1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4 Write a line of code that outputs the phase Hello World to the console without creating a new line character. System.out.print(

More information

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved.

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved. 1 1 Introduction to OOP Using Java 2 Introduction Sun s implementation called the Java Development Kit (JDK) Object-Oriented Programming Java is language of choice for networked applications Java Enterprise

More information