Review and Java Questions 13 June 2009. Classes, objects, attributes, methods In Java, by convention a class name begins with Uppercase: Coffee, String.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Static Class Members Wrapper Classes Autoboxing Unboxing.
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology- George Koutsogiannakis.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Class Library, Formatting, Wrapper Classes, and JUnit Testing
ARRAYLIST Collections of Data. ArrayLists Array lists can grow and shrink as needed ArrayList is a generic class (similar to C++ template) ArrayList has.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Day 8 Integers, Remainders, and other Primitive Types.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
1 Even even more on being classy Aaron Bloomfield CS 101-E Chapter 4+
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double.
Types in programming languages1 What are types, and why do we need them?
© A+ Computer Science - Variables Data Types © A+ Computer Science - Lab 0B.
Arrays…JavaCPython have fixed lengthyes*yesno are initialized to default values yesno? track their own lengthyesnoyes trying to access “out of bounds”
Introduction to Java Primitive Types Operators Basic input and output.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.4 Using Java Built-in Classes Produced by.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
1 Printing characters : Revisited class printcharacter{ public static void main(String arg[]){ char grade=‘A’; System.out.println(grade);// prints A System.out.println((int)grade);
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
Java Programming. Objects  Java is an object-oriented programming language –use objects to define both the data type and the operations that can be applied.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
JAVA MULTIPLE CHOICE QUESTION.
Review of Java … or maybe not.
Introduction to Java part 2
Chapter 4 – Fundamental Data Types
Exercise on Java Basics
Coding Standard & Javadoc
Exercise on Java Basics
5 Variables, Data Types.
Unit-2 Objects and Classes
Introduction to Java part 2
Chapter 2: Java Fundamentals
Introduction to Java part 2
Using java libraries CGS3416 spring 2019.
Subtype Substitution Principle
Presentation transcript:

Review and Java Questions 13 June 2009

Classes, objects, attributes, methods In Java, by convention a class name begins with Uppercase: Coffee, String a method name uses camelCase: getMoreCoffee( ) a method has ( ) variable names also use camelCase: myCoffee constants use UPPER_CASE and _: MAX_COFFEE package names are all lowercase (but not always) primitive types are all lowercase: boolean, int, double

Identify each of these Is it a package class primitive type attribute ("field") (static or instance) method (static or instance) constant interface (more advanced) ??? Date System.out System.out.println( ) System.nanoTime( ) double Double java.lang.Double.MAX_VALUE java.lang.BigInteger java.lang.Comparable java.text java.util.ArrayList java.util.List

Packages  Java uses packages to organize classes.  Packages reduce size of name space and avoid name collisions (like Date in java.util and java.sql ). Q: Which package contain these classes? Java language core classes (Object, String, System,...). You never have to "import" this. Classes for input and output, like InputStream Date classes, collections (List, ArrayList), utilities Scanner, Arrays Java Graphics frameworks (2 packages)

Resolving Ambiguity  There is a java.util.Date class and java.sql.Date  In this code, which Date class will Java use? 1. java.util.Date (because it was imported first) 2. java.sql.Date (because it was imported last) 3. depends on Java implementation 4. neither -- compiler error if ambiquous import java.util.*; import java.sql.*; class Ambiguous { Date today = new Date( );... }

Resolving Ambiguity (2)  How can you resolve this problem (2 solutions)? Solution 1: Solution 2: import java.util.*; import java.sql.*; import java.util.Date; // (1) class Ambiguous { java.util.Date today = new java.util.Date( ); // (2)... }

How to convert number to String  How can you convert n to a String (many solutions)? int n = 100; String s = n; // error: must convert to string s1 = Integer.toString( n ); s2 = "" + n; s3 = String.valueOf( n ); s4 = String.format( "%d", n );

Java Primitive Data Types  Java has 8 primitive data types  These are not classes or objects. No properties. NameValuesExamples booleantrue falsetrue, false charcharacter'a', 'A', '1', ' ก ', ' ค ', ' โ ', '\t' byte 8-bit integer -127,..., -1, 0, 1,..., 127 short16-bit integer int int32-bit integer long64-bit integer L 0L 888L floatdecimal F 0.0F -2.5E-8F double64-bit decimal E234

How to Convert Primitive to Object? A List (like ArrayList) can only contain objects. How can we convert a primitive type to an object? List mylist = new ArrayList( ); int id = ; mylist.add( id ); // works in Java 5+, but something is happening

Wrapper Classes PrimitiveWrapper booleanBoolean charCharacter byteByte shortShort intInteger longLong floatFloat doubleDouble double root = Math.sqrt( 2.0 ); Double d1 = new Double( root ); // same thing: automatic boxing Double d2 = root; // print as a string out.println( d2.toString( ) ); // static method to make a string out.println( Integer.toString( 2 ) );

Wrapper to convert to/from String int n = 2*3*5*7*11*13*17*19*23*29*31; // convert n to a String String product = Integer.toString(n); String s = "2.5"; // convert s to a double?

Numeric wrappers: range constants  What is the largest "int" value?  What is the smallest "long" value?  What is the range (smallest, biggest) of double? int biggest = long smallest = double minsize = double maxsize =

What happens if you go beyond? int n = Integer.MAX_VALUE; n = n + 1; System.out.println( n ); double d = Double.MAX_VALUE; d = d + 1; System.out.println( d ); d = d * ; System.out.println( d );

C# is different  "int", "float", "double" are struct types. // This is C# int n = int.MaxValue; String s = "The biggest integer is " + n.ToString( ) ; // range checking is enforced n = n + 1; System.OverflowException: Arithmetic operation resulted in an overflow.