1 TP #4 n Last TP exercises solved; n Parsing command line arguments; n Static; n Wrapper classes for primitive types; n Package visibility.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Mar 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

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.
DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Wrappers: Java’s Wrapper Classes for the Primitives Types Steve Bossie.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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.
IC211 Lecture 8 I/O: Command Line And JOptionPane.
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
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.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Moving from C to Java. The Java Syntax We Know Java Types Some of the types we know and love are still there  Integer: byte, short, char, int, long.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4.
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
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.
Mixing integer and floating point numbers in an arithmetic operation.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Computer Science 320 Massive Parallelism. Example Problem: Breaking a Cipher Somehow obtain a sample plaintext and its ciphertext Then search for the.
CONTENTS Wrapper Class Enumeration Garbage Collection Import static.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
CSC Java Programming, Spring, 2010 Week 2: Java Data Types, Control Constructs, and their C++ counterparts.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Object-Oriented Programming Fundamentals. Comp 241, Fall Example Colored points on the screen What data goes into making one? Coordinates Color.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Object Oriented Programming Lecture 2: BallWorld.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Java Basics Classes and Objects.
Formatting Output & Enumerated Types & Wrapper Classes
Elementary Programming
Static Members and Methods
Yanal Alahmad Java Workshop Yanal Alahmad
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Objects, Classes, Program Constructs
FUNDAMENTALS OF JAVA.
TK1114 Computer Programming
An Introduction to Java – Part I
INPUT STATEMENTS GC 201.
CMSC 202 Static Methods.
5 Variables, Data Types.
Unit-2 Objects and Classes
Chapter 3, cont Sept 20, 2004.
Introduction to Java Programming
An Introduction to Java – Part I, language basics
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
Java so far Week 7.
Classes and Objects 5th Lecture
class PrintOnetoTen { public static void main(String args[]) {
Programs and Classes A program is made up from classes
Java Programming Review 1
Classes and Objects Static Methods
Today in COMP 110 Brief static review The Math Class Wrapper Classes
CSC Java Programming, Spring, 2012
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

1 TP #4 n Last TP exercises solved; n Parsing command line arguments; n Static; n Wrapper classes for primitive types; n Package visibility.

© L.Lúcio, Last TP exercises solved n The Logger class: package ch.unige.cours.langages.TP3; public class Logger { /** Internal priority of a logger object */ private int priority; public Logger(int priority){ this.priority = priority; } /** Change priority */ public void setPriority(int priority) { this.priority = priority; } /** Print a message if its priority is more important than the logger's priority */ public void print(String msg, int msgPriority) { if (msgPriority <= priority) System.out.println(msg); }

© L.Lúcio, Last TP exercises solved (2) n Example of usage: import ch.unige.cours.langages.TP3.*; public class Test { public static void main(String[] args) { if (args.length != 1) { System.out.println("You must pass the verbosity level to the application..."); System.exit(-1); } /* parse the level of priority from the command line */ int i = Integer.parseInt(args[0]); /* create the logger object with the adequate priority */ Logger log = new Logger(i); log.print("This is a message with priority 0",0); log.print("This is a message with priority 1",1); log.print("This is a message with priority 2",2); log.print("This is a message with priority 3",3); }

© L.Lúcio, Last TP exercises solved (3) n Command line execution:

© L.Lúcio, Static n How does the following command work? int i = Integer.parseInt(args[0]); n The parseInt method of the Integer class is defined as static, so it can be called without an instance of the class… n In a class, both data and methods that are static are common to all instances of the class: class myClass { static int counter=0; int someData; static void incr() { counter++; }

© L.Lúcio, Static (2) n What would be the output of the previous program? n Would it make sense for the static incr() method to access someData? n Why is the main (entry point for a java program) static? MyClass my1 = new MyClass(); MyClass my2 = new MyClass(); MyClass.incr(); System.out.println(my1.counter); System.out.println(my2.counter); System.out.println(MyClass.counter);

© L.Lúcio, Wrapper classes for primitive types int i = Integer.parseInt(args[0]); n In the java.lang package java provides wrappers for all the primitive types: Boolean, Byte, Character, Double, Float, Integer, Long, and Short n Useful if it is necessary to use primitive types as objects; n Provide utilities (e.g. parseInt) to ease the life of the programmer; n Provide constants about the type (e.g. max, min values); n Check the javadoc (on the web) for more information…