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.

Slides:



Advertisements
Similar presentations
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Written by: Dr. JJ Shepherd
 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.
Road Map Introduction to object oriented programming. Classes
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Session 4 Command-Line Arguments, Strings, and Files.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
Computer Science II 810:062 Section 01 Session 3 - Objects and Responsibilities.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Computer Science II 810:062 Section 01. How is CS I different from CS II? When you teach Java there are a series of decisions that have to be made…
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Simple Arrays Arrays of primitives and Strings Sections 7.1, 7.2.
Chapter 7: Class Variables and Methods Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 7 Class.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CSE 1201 Object Oriented Programming ArrayList 1.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Polymorphism l Constructors.
Written by: Dr. JJ Shepherd
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
OOP Basics Classes & Methods (c) IDMS/SQL News
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Chapter VII: Arrays.
The need for Programming Languages
Chapter 10 – Exception Handling
Some Eclipse shortcuts
Control Statement Examples
CHAPTER 6 GENERAL-PURPOSE METHODS
Arrays and Array Lists CS 21a.
slides created by Ethan Apter
Java Programming Language
Classes, Objects and Methods
Visibilities and Static-ness
Presentation transcript:

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 Pro: the most versatile. –Use System.in Con: less versatile then event-driven Pro: requires less new code –Use the command line (String[ ] args) Con: very limited in its use Pro: the simplest to set up

Using the command line Remember what causes the “big bang” in our programs?

Using the command line Remember what causes the “big bang” in our programs? public static void main (String [] args) {

Using the command line Remember what causes the “big bang” in our programs? public static void main (String [] args) { main expects an array of strings as a parameter. The fact of the matter is that this array has been a null array in each of our programs so far.

Using the command line However, we can give this array values by providing command line arguments when we start a program running.

Using the command line However, we can give this array values by providing command line arguments when we start a program running. java MyProgram String1 String2 String3

Using the command line However, we can give this array values by providing command line arguments when we start a program running. $ java MyProgram String1 String2 String3 args[0] args[1] args[2]

Using the command line We can use this to get information from the user when the program is started: public class Echo { public static void main(String [] args) { System.out.println(“args[0] is “ + args[0]); System.out.println(“args[1] is “ + args[1]); } // end main } // end Echo class $ javac Echo.java $ java Echo Mark Fienup args[0] is Mark args[1] is Fienup

What are some of the problems with this solution This works great if we “behave” and enter two arguments. But what if we don’t? $ java Echo Mark Alan Fienup args[0] is Mark args[1] is Alan (no problem, but Fienup gets ignored) $ java Echo Mark args[0] is Mark Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: (Big problem!)

Fixing this problem There are several ways to work around this problem –Use Java’s exception handling mechanism (not ready to talk about this yet) –Write your own simple check and handle it yourself public class MyEcho2 { public static void main( String[] args ) { if (args.length == 2) { System.out.println("args[0] is ” + args[0]); System.out.println("args[1] is " + args[1]); } else { System.out.println( "Usage: java MyEcho2 " + "string1 string2"); } // end if } // end main } // end MyEcho2

Fixing this problem public class MyEcho2 { public static void main( String[] args ) { if (args.length == 2) { System.out.println("args[0] is ” + args[0]); System.out.println("args[1] is " + args[1]); } else { System.out.println( "Usage: java MyEcho2 " + "string1 string2"); } // end if } // end main } // end MyEcho2 $ java MyEcho2 Mark Usage: java MyEcho2 string1 string2 $ java MyEcho2 Mark Alan Fienup Usage: java MyEcho2 string1 string2

I learned something new! Will this code work if the user types NO arguments: “ java MyEcho2 ”? public class MyEcho2 { public static void main( String[] args ) { if (args.length == 2) { System.out.println("args[0] is ” + args[0]); System.out.println("args[1] is " + args[1]); } else { System.out.println( "Usage: java MyEcho2 " + "string1 string2"); } // end if } // end main } // end MyEcho2

Yes! The args array reference could be “null”, so doing args.length would cause an error! But it is not, since args is an array reference to an actual array with zero elements.

What about? public class TestArray { public static void main( String[] args ) { System.out.println("args.length = " + args.length ); String[] temp0 = {}; System.out.println( "temp0.length = " + temp0.length ); String[] tempNull; System.out.println("tempNull.length=" + tempNull.length); } // end main } // end TestArray class $ javac TestArray.java TestArray.java:7: variable tempNull might not have been initialized System.out.println( "tempNull.length = " + tempNull.length ); ^ 1 error

What about? public class TestArray { public static void main( String[] args ) { System.out.println("args.length = " + args.length ); String[] temp0 = {}; System.out.println( "temp0.length = " + temp0.length ); String[] tempNull = null; System.out.println("tempNull.length=" + tempNull.length); } // end main } // end TestArray class $ java TestArray args.length = 0 temp0.length = 0 Exception in thread "main" java.lang.NullPointerException at TestArray.main(TestArray.java:7)

Your turn to write a simple program using the command line Write a program to echo all command-line arguments to the System.out $ java EchoAll This is a long line args[0] is This args[1] is is args[2] is a args[3] is long args[4] is line

Simple Calculations at Command Line $ java Calculate $ java Calculate

First Attempt - What’s wrong? public class Calculate { public static void main( String[] args ) { int operand1 = args[0]; String operator = args[1]; int operand2 = args[2]; if ( operator.equals("+") ) { System.out.println( operand1 + operand2 ); } else if ( operator.equals("-") ) { System.out.println( operand1 - operand2 ); } else { System.out.println("Invalid operator: " + operator); } // end if } // end main } // end Calculate $ javac Calculate.java Calculate.java:3: incompatible types found : java.lang.String required: int int operand1 = args[0]; ^

Correct Calculate public class Calculate { public static void main( String[] args ) { int operand1 = Integer.parseInt( args[0] ); String operator = args[1]; int operand2 = Integer.parseInt( args[2] ); if ( operator.equals("+") ) { System.out.println( operand1 + operand2 ); } else if ( operator.equals("-") ) { System.out.println( operand1 - operand2 ); } else { System.out.println( "Invalid operator: " + operator ); } // end if } // end main } // end Calculate

The wrapper classes Primitive data types (int, double, boolean, etc.) are not actually objects. Because of this, you can’t use them easily in certain OO situations Because of that, java has “wrapper classes” such as Integer, Double, and Boolean. These are true classes in the OO sense of the word in that they contain data which store information (often the value in it’s corresponding primitive data type) and methods that can act on this data.

But wait a minute! How is it that we can use the parseInt() method without actually creating an instance of the Integer class.

Lifetime Modifiers What does static mean?

Lifetime Modifiers What does static mean? The item being declared is a feature of the class – what we usually call “class methods” or “class variables.” The item being declared exists at load time, before any instance is created.

Lifetime Modifiers A “class method” is one that can be invoked without sending a message to an instance of the class. the main method of MemoPadApp int operand1 = Integer.parseInt(args[0]); double myRandom = Math.random();

Lifetime Modifiers A “class variable” is one that every instance has access to and shares. In chapter 5, Budd creates windows in which bouncing balls live. Every instance of his BallWorld class shares the same height and width dimensions, implemented as static class variables: public static int frameWidth=200; public static int frameHeight=250;

Lifetime Modifiers We will use these rarely in the code we write. –The more you use static stuff, the less flexible and modifiable your code tends to be. The Java class library uses these more frequently. Budd will use them occasionally. –Thus, you will still get to see plenty of examples before we are done!

Homework #2 - Implementing Constructors Goals for this assignment:  practice implementing constructors  practice factoring common behavior into helper methods  experience working with command-line arguments You will continue to work with the MemoPad program for this assignment, including the database class that you implemented for Homework 1

Homework #2 - Task 1 1. Add a new constructor to your MemoDatabase class that takes one argument: the maximum number of memos that can be stored. The constructor should use this argument when initializing the size of the array in the constructor. The existing constructor should still use a default value for the array size. Notice that the constant now becomes the default maximum, not the actual maximum! Test your new constructor by using it to create the database in the MemoPad class and then running the program. Verify that the database enforces the maximum size! Make sure that the class's default constructor still works, too, and that it still enforces its maximum number of entries.

Homework #2 - Task 2 2. Eliminate any code duplication in your MemoDatabase class's constructors. Your two constructors almost certainly have a lot of code common -- they do almost exactly the same thing! Make the duplication go away. One way to do that is to factor any common behavior into a private method named initialize. Re-test both constructors by using them to create the database in the MemoPad class and then running the program.

Homework #2 - Task 3 3. Add a new constructor to the MemoPad class that takes one argument: the MemoDatabase that it uses to store the memos. In this constructor, use the argument to initialize the database instance variable. Test your new constructor by using it to create the MemoPad in the main() method and then running the program.

Homework #2 - Task 4 4. Change the original "default" constructor back to its original form, from before Homework 1: initialize the database variable to be a DefaultMemoDatabase.Homework 1 Test the default constructor by using it to create the MemoPad in the main() method and then running the program.

Homework #2 - Task 5 5. Eliminate any code duplication in the MemoPad constructors. The two constructors almost certainly have a lot of code common -- they differ in only one line! Make the duplication go away. One way to do that is to factor any common behavior into a private method named initialize. Re-test both constructors by using them to create the MemoPad in the main() method and then running the program.

Homework #2 - Task 6 6. Modify the main() method in the MemoPadApp driver to accept up to two optional arguments that control the kind of MemoDatabase to be used. One optional argument is the choice of database class. -d indicates to use the default implementation. -s indicates to use the student implementation.

Homework #2 - Sample Executions If no argument is given, create a default instance of MemoPad. For example: $ java MemoPadApp... MemoPadApp uses the default constructor of MemoPad

Homework #2 - Sample Executions If the user specifies -d, create an instance of DefaultMemoDatabase and use it to initialize the MemoPad you create. For example: $ java MemoPadApp -d... MemoPadApp creates a DefaultMemoDatabase and passes it to MemoPad's constructor

Homework #2 - Sample Executions If the user specifies -s, then the command- line may contain a second optional argument, an integer to specify the maximum number of entries in the database. Use this integer to create an instance of your database class. If no second argument is given, create a default instance of your array-based database class. In either case, use this database object to initialize the MemoPad you create.

Homework #2 - Sample Executions $ java MemoPadApp -s... MemoPadApp creates an instance of your MemoDatabase (use default constructor) and passes it to MemoPad's constructor $ java MemoPadApp -s MemoPadApp creates an instance of your MemoDatabase (use the int constructor) and passes it to MemoPad's constructor

Homework #2 - Sample Executions If the user gives any other command-line argument, print an error message and return without creating any objects. For example: $ java MemoPadApp -oops Usage: java MemoPadApp java MemoPadApp -d java MemoPadApp -s [size]