CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.

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

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
 It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
11-Jun-15 Just Enough Java. 2 What is Java? Java is a programming language: a language that you can learn to write, and the computer can be made to understand.
Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.
Access to Names Namespaces, Scopes, Access privileges.
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
20-Jun-15 Methods. Complexity The programmer's biggest adversary is complexity The programmer's biggest adversary is complexity Primitive types and the.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling the.
Just Enough Java 17-Apr-17.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
29-Jun-15 Using Objects. 2 Classes and objects The type of an object is the class that describes that object If we say int count, the type of count is.
30-Jun-15 Static Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
CIT 590 Intro to Programming First lecture on Java.
COMP More About Classes Yi Hong May 22, 2015.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
CIT 590 Intro to Programming First lecture on Java.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
CPS120: Introduction to Computer Science Decision Making in Programs.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
10-Nov-15 Java Object Oriented Programming What is it?
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
OOP Basics Classes & Methods (c) IDMS/SQL News
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Topic: Classes and Objects
User-Written Functions
Suppose we want to print out the word MISSISSIPPI in big letters.
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Namespaces, Scopes, Access privileges
Exceptions 10-Nov-18.
Static Methods 14-Nov-18.
Class Structure 16-Nov-18.
CSC 113 Tutorial QUIZ I.
Class Structure 28-Nov-18.
CS/ENGRD 2110 Fall 2017 Lecture 5: Local vars; Inside-out rule; constructors
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Recap Week 2 and 3.
Namespaces, Scopes, Access privileges
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Class Structure 25-Feb-19.
Classes, Objects and Methods
Barb Ericson Georgia Institute of Technology Oct 2005
Just Enough Java 17-May-19.
Loops CGS3416 Spring 2019 Lecture 7.
Classes and Methods 15-Aug-19.
How to Run a Java Program
Presentation transcript:

CIT 590 Intro to Programming Lecture 13

Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit Hold control down and mouse click to navigate to functions/classes etc Outline Compare with … local history (life saver!)

String concatenation in Java What does the following do? int x = 45; int y = 1; String s = x + y + “ is x plus y and “ + x + y + “ is also x plus y”;

Loudcrowd multiple choice question int x = 45; double y = 0.2; boolean z = true; String s1 = “abc” + x + z + y; What is s1? a) syntax error c)abc45.2true e) abc45true0.2 b) abc45true.2 d)abc45true.2

Loudcrowd ++ int n1 = 23; int n2 = 0; int temp = 0; while (n1 > 0){ temp = ++n1 % 10; n2 = n2++ * 10 + temp; this has no effect on n2 n1 = n1/10; } System.out.println(n1 + n2); What gets printed? 43

Test driven development in Java We’ll spend some time writing unit tests for the Rational class Along the way, we will try and learn other Java concepts as well.

7 JUnit in Eclipse If you write your method stubs first (as on the previous slide), Eclipse will generate test method stubs for you To add JUnit 4 to your project: Select a class in Eclipse Go to File  New...  JUnit Test Case Make sure New JUnit 4 test is selected Click where it says “ Click here to add JUnit 4... ” Close the window that appears To create a JUnit test class: Do steps 1 and 2 above, if you haven’t already Click Next> Use the checkboxes to decide which methods you want test cases for; don’t select Object or anything under it I like to check “create tasks,” but that’s up to you Click Finish To run the tests: Choose Run  Run As  JUnit Test

8 Viewing results in Eclipse Bar is green if all tests pass, red otherwise Ran 10 of the 10 tests No tests failed, but... Something unexpected happened in two tests This test passed Something is wrong Depending on your preferences, this window might show only failed tests This is how long the test took

9 Defining constructors A constructor is code to create an object You can do other work in a constructor, but you shouldn’t The syntax for a constructor is: ClassName ( parameters ) { …code… } The ClassName has to be the same as the class that the constructor occurs in The parameters are a comma-separated list of variable declarations

10 Example constructor I public class Person { String name; int age; boolean male; Person (String aName, boolean isMale) { name = aName; male = isMale; } }

Most constructors just set instance variables: public class Person { String name; boolean male; Person (String name, boolean male) { this.name = name ; this.male = male ; } } 11 Example constructor II this is needed to resolve ambiguity

Defining a method A method has the syntax: return-type method-name ( parameters ) { method-variables code } Example: boolean isAdult(int age) { int magicAge = 21; return age >= magicAge; } Example: double average(int a, int b) { return (a + b) / 2.0; }

13 Methods may have local variables A method may have local (method) variables Formal parameters are a kind of local variable int add(int m, int n) { int sum = m + n; return sum; } m, n, and sum are all local variables The scope of m, n, and sum is the method These variables can only be used in the method, nowhere else The names can be re-used elsewhere, for other variables

14 Blocks (== Compound statements) Inside a method or constructor, whenever you use braces, you are creating a block, or compound statement: int absoluteValue(int n) { if (n < 0) { return -n; } else return n; }

15 Nested scopes 1 int fibonacci(int limit) { 2 int first = 1; 3 int second = 1; 4 while (first < 1000) { 5 System.out.print(first + " "); 6 int next = first + second; 7 first = second; 8 second = next; 9 } 10 System.out.println( ); 11 }

16 Declarations in a method The scope of formal parameters is the entire method The scope of a variable in a block starts where you define it and extends to the end of the block if (x > y) { int larger = x; } else { int larger = y; } return larger; Scoped to the if block Scoped to the else block Illegal 

17 The for loop The for loop is a special case You can declare variables in the for statement The scope of those variables is the entire for loop This is true even if the loop is not a block void multiplicationTable() { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) System.out.print(" " + i * j); System.out.println(); } }

Returning a result from a method If a method is to return a result, it must specify the type of the result: boolean isAdult ( … You must use a return statement to exit the method with a result of the correct type: return age >= magicAge ;

Returning no result from a method The keyword void is used to indicate that a method doesn’t return a value The return statement must not specify a value Example: void printAge(String name, int age) { System.out.println(name + " is " + age + " years old."); return; }  There are two ways to return from a void method:  Execute a return statement  Reach the closing brace of the method

20 Sending messages to objects We don’t perform operations on objects, we “talk” to them This is called sending a message to the object A message looks like this: object. method ( extra information ) The object is the thing we are talking to The method is a name of the action we want the object to take The extra information is anything required by the method in order to do its job Examples: g.setColor(Color.pink); amountOfRed = Color.pink.getRed( );

21 Putting it all together class Person { // fields String name; int age; // constructor Person(String name) { this.name = name; age = 0; } // methods String getName() { return name; } void birthday() { age = age + 1; System.out.println( "Happy birthday!"); } }

22 Using our new class Person john; john = new Person("John Smith"); System.out.print (john.getName()); System.out.println(" is having a birthday!"); john.birthday();  Of course, this code must also be inside a class!

23 Diagram of program structure A program consists of one or more classes Typically, each class is in a separate.java file Program File Class Variables Constructors Methods Variables Statements

24 null If you declare a variable to have a given object type, for example, Person john; String name;...and if you have not yet assigned a value to it, for example, with john = new Person(); String name = “John Smith";...then the value of the variable is null null is a legal value, but there isn’t much you can do with it It’s an error to refer to its fields, because it has none It’s an error to send a message to it, because it has no methods The error you will see is NullPointerException

Methods and static methods Java has two kinds of methods: static methods and non- static methods (called instance methods) However, before we can talk about what it means to be static, we have to learn a lot more about classes and objects Most methods you write should not, and will not be static Every Java program has a public static void main(String[ ] args) method This starts us in a “static context” To “escape from static”, I recommend starting every program in a certain way, as shown on the next slide

Escaping from static class MyClass { public static void main(String[] args) { new MyClass().run(); } void run() { // Your real code begins here } } You can replace the names MyClass and run with names of your choice, but notice that each name occurs in two places, and they have to match up Do not worry about this for the current assignment!

Default values/ default constructors Do I have to have a constructor? No. Java will provide one You might not like what Java provides and hence usually best to write your own What if I have a class with 25 fields, but most of them have a default value Call the complicated constructor with default values this(argument1, default value of argument 2)