CECS 220 Java Test Review. Variable Names 0 May Contain: a-z, A-Z, 0-9, _, $, characters from other languages. 0 May not start with 0-9. 0 Legal: 0 MyVariable.

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

the java language BY SA introduced in 1995 by Sun Microsystems.
Java Review Interface, Casting, Generics, Iterator.
Written by: Dr. JJ Shepherd
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
23-May-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
Access to Names Namespaces, Scopes, Access privileges.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
1 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
CS2110: Software Development Methods Readings: PDF on website: 1 st edition of MSD text, Appendix A. This covers: Structure of Java program; data types;
CS 61B Data Structures and Programming Methodology July David Sun.
The Ruby Programming Language
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture objectives  Differences between Java and C#  Understand and use abstract classes  Casting in Java: why is it important?  Master exceptions.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Data Structures and Java CS 105. L7: Java Slide 2 Data structure Data structure defined: A systematic way of organizing and accessing data Examples Dictionary:
CS/ENGRD 2110 SPRING 2015 Lecture 6: Consequence of type, casting; function equals 1.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CS/ENGRD 2110 FALL 2014 Lecture 6: Consequence of type, casting; function equals 1.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Topic 4 Inheritance.
Types in programming languages1 What are types, and why do we need them?
Objects & Classes Weiss ch. 3. So far: –Point (see java.awt.Point) –String –Arrays of various kinds –IPAddress (see java.net.InetAddress) The Java API.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Written by: Dr. JJ Shepherd
CS/ENGRD 2110 FALL 2015 Lecture 6: Consequence of type, casting; function equals 1.
Classes Revisited Chapter 8.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
BY:- TOPS Technologies
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Namespaces, Scopes, Access privileges
More inheritance, Abstract Classes and Interfaces
null, true, and false are also reserved.
Polymorphism.
An overview of Java, Data types and variables
Simple Classes in Java CSCI 392 Classes – Part 1.
Namespaces, Scopes, Access privileges
CSC 111 Exam 3 Review Fall 2005.
TCSS 143, Autumn 2004 Lecture Notes
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

CECS 220 Java Test Review

Variable Names 0 May Contain: a-z, A-Z, 0-9, _, $, characters from other languages. 0 May not start with Legal: 0 MyVariable 0 myVariable 0 MYVARIABLE 0 x 0 _my_variable 0 $myvariable 0 ανδρος 0 This_is_an_insanely_long_variable_name_that_never_ends 0 Not Legal: 0 My Variable 0 9pins 0 a+c 0 #include 0 O’Reilly 0 Oreilly_&_Associates

Function, or procedure? void calculate() { int value = 5 * 4; return; } int calculate() { int value = 5 * 4; return value; } void calculate(int a, int b) { if (a == 5 && b == 4) { System.out.println(20); return; } System.out.println(a * b); }

Testing 0 JUnit vs not using an automated framework 0 Reference utilities.CalculatorTest vs utilities.testing.TestCalculator

To be or not to be static? It's a matter of belonging. Static = belongs to the class, not static = belonging to an instance of the class. class Vehicle{ // Do these variables apply to vehicles in general, or to a specific one? private int numWheels= 4 ; private static int numPeople = 5; } class Motorcycle{ private static int numWheels= 2 ; private int numPeople = 2; }

To be or not to be static? 0 Same applies to functions and procedures 0 Should eat() be a function of apples in general, or a specific one? 0 Should getNumberOfLegs() be a function of cats in general, or a specific one?

Packages 0 For organization 0 package tools; public class Calculator{ public static int multiply(int a, int b) { return a * b; } } 0 package tools; public class AnotherTool{ public static void function() { } } 0 package stuff; public class Calculator{ public static void doSomething() { } }

Packages 0 package com.example.tools; public class Calculator { public static int multiply(int a, int b) { return a * b; } } 0 com.examples.tools.Calculator.multiply(4, 5); 0 import com.examples.tools.Calculator;... Calculator.multiply(4, 5);

Class, instances of class 0 JFrame f = new JFrame(); f.add(...); JFrame.add(...); ??? 0 Double num = new Double(5.5); double value = num.doubleValue(); double value2 = Double.doubleValue(); ??? 0 double value = Double.parseDouble("5.5"); double value2 = value.parseDouble("5.5"); ???

this 0 class Foo { private static final int DEFAULT = 5; private int myNumber; public Foo() { this(DEFAULT); // ?? } public Foo(int number) { myNumber = number; } }

this 0 class Foo { private static final int DEFAULT = 5; private int number; public Foo() { this(DEFAULT); } public Foo(int number) { this.number= number; // ?? } }

Extending classes 0 class Animal { private string name= "animal"; public Animal(string name) { this.name = name; } public string getName() { return myName; } } 0 class Dog extends Animal { private string name= "dog"; public Dog(string name) { super(name); // ?? } } 0 System.out.println(new Dog("Caesar").getName()); // ???

Scope 0 For a gun? For a clean mouth? 0 // Which variable is which?? class Foo { private double variable; public Foo(double variable) { this.variable = variable; // "this"?? // variable = variable; // Why is this incorrect? } public boolean compare(double variable) { if (this.variable == variable) { return true; } return false; } }

Scope 0 Where do variables live? Inside curly braces 0 class Foo { private double variable1 = 1.0; void function1() { double variable2 = 2.0; variable2 = variable1; } void function2() { double variable3 = 3.0; variable1 = variable3; variable3 = variable2; // ??? } }

Scope 0 Where do variables live? 0 void foo() { int a = 5; { int b = 6; a = b; } b = a; // ??? }

If 0 These are identical: 0 int a =...; if (a 0) {... } 0 int a =...; if (a < 0) {... } else if (a == 0) {... } else {... }

For 0 These are identical: 0 int[] array =...; 0 for (int i = 0; i < array.length; i++) { int element = array[i]; System.out.println(element); } 0 for (int element : array) { System.out.println(element); }

Casting 0 Bronze? For an arm? Throwing? 0 Object o = (Object)(new Integer(5)); 0 Animal a = new Dog(); // Dog extends Animal 0 This is "casting up the class hierarchy"

Casting 0 What about "casting down the class hierarchy"? 0 HondaCivic h= new Car(); // extends Car /* Wrong, and will fail at runtime! All HondaCivics have all the parts of a general car because HondaCivic extends Car, but there might be things (i.e. fields) that the HondaCivic class has in addition. Would the computer just make up things to fill these gaps? */ 0 HondaCivic h= (HondaCivic)((Car)(new HondaCivic(1994, "blue"))); /* This works though, and won't fail at runtime. The "real type" stays the same even though the "apparent type" changes */ 0 Protect with "if (c instanceof HondaCivic)..."

Wrapper classes 0 ArrayList aListOfNumbers =... // ??? 0 ArrayList aListOfNumbers =... 0 Remember "all classes are subclasses of Object"? Actually, the primitives aren't; without wrappers, this causes problems for things like ArrayLists that only work for subclasses of Object

Debugging 0 It's why there are best practices and good habits. You'll learn them over time 0 Maybe most common: 0 class Foo { int DEFAULT = 5; private int value = DEFAULT; public Foo(int value) { value = value; this.value == value; value = this.value; } public boolean isSameAs(int otherValue) { return value == otherValue; } public boolean isSameAs2(int otherValue) { return value = otherValue; } public void theProcedureIForgotToComplete() { } } 0 System.out.println(new Foo(4).isSameAs(5)); // Prints "true"??? 0 System.out.println(new Foo(4).isSameAs2(10)); // Prints "true"??? 0 new Foo(4).theProcedureIForgotToComplete(); // Won't work???

Arrays 0 2D array: 0 "Student ID" and "Student Name" aren't actually a part of it, that's just what we're arbitrarily labeling the columns: Student ID Student Name 1 Bob 2 Sue 3 Ann

Arrays 0 In Java, a 2D array is actually a 1D array of arrays: 0 int[,] array = new int[,] {{0, 2, 4}, {1, 3, 5}}; 0 [[0], [1]] [2] [3] [4] [5] 0 3D arrays are an array of arrays of arrays. Etc 0 You can have "jagged" arrays in Java: int[,] array = new int[,] {{0}, {0, 1, 2}}; string[] months = new string[] {"January", "Feb"};

GUI and ActionListeners 0 See project; set option in main() to DisplayOptions.ShowForms and run

Exceptions 0 class Int { public static int Parse(string s) { if (the string can't be turned into an int) { throw new Exception("Invalid string"); } return (the string as an int); } } 0 try { int i = Int.Parse("5.5"); } catch (Exception e) { System.out.println(e.getMessage()); }

Making Exceptions 0 Just extend from the Throwable class: 0 class MeaningLessException extends Throwable {... } 0 if (...) { throw new MeaningLessException(...); }

vs Error 0 Errors represent something more critical, something "unrecoverable"

Garbage collection 0 PPT 3 Slide 16

Questions?