The User Experience Java Programming 3 Lesson 2

Similar documents
Java Programming Constructs Java Programming 2 Lesson 1

Decisions: Logic Java Programming 2 Lesson 7

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Super-Classes and sub-classes

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

COMP-202 More Complex OOP

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Introduction to Programming Using Java (98-388)

Slide 1 CS 170 Java Programming 1

Using the API: Introductory Graphics Java Programming 1 Lesson 8

COMP-202 Unit 4: Programming with Iterations

Chapter 1 Getting Started

Tips from the experts: How to waste a lot of time on this assignment

The Stack, Free Store, and Global Namespace

Tips from the experts: How to waste a lot of time on this assignment

Chapter 10 Recursion

St. Edmund Preparatory High School Brooklyn, NY

Warmup : Name that tune!

6.001 Notes: Section 8.1

Java Coding 3. Over & over again!

Chapter 4 Defining Classes I

Tips from the experts: How to waste a lot of time on this assignment

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

Hi everyone. Starting this week I'm going to make a couple tweaks to how section is run. The first thing is that I'm going to go over all the slides

Linked Lists. What is a Linked List?

COMP-202 Unit 9: Exceptions

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Post Experiment Interview Questions

COMP-202 Unit 9: Exceptions

MITOCW watch?v=w_-sx4vr53m

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Math Modeling in Java: An S-I Compartment Model

Intro. Speed V Growth

Create your first workbook

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Troubleshooting Maple Worksheets: Common Problems

This example uses a Web Service that is available at xmethods.net, namely RestFulServices's Currency Convertor.

From time to time Google changes the way it does things, and old tutorials may not apply to some new procedures.

CS103 Spring 2018 Mathematical Vocabulary

1 Shyam sir JAVA Notes

Lecture 5: Methods CS2301

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

AP Computer Science Unit 1. Programs

Java and OOP. Part 2 Classes and objects

There are some situations in which recursion can be massively inefficient. For example, the standard Fibonacci recursion Fib(n) = Fib(n-1) + Fib(n-2)

JUnit Test Patterns in Rational XDE

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

MITOCW watch?v=zlohv4xq_ti

Who am I? I m a python developer who has been working on OpenStack since I currently work for Aptira, who do OpenStack, SDN, and orchestration

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

COMP 202 Java in one week

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

CS111: PROGRAMMING LANGUAGE II

CSE 114 Computer Science I

Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key

4. Java Project Design, Input Methods

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

BBC Learning English Face up to Phrasals Mark's Mistake

EXCEPTIONS. Fundamentals of Computer Science I

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

CSE 142/143 Unofficial Commenting Guide Eric Arendt, Alyssa Harding, Melissa Winstanley

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

A Foundation for Programming

CS110D: PROGRAMMING LANGUAGE I

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Introduction to Software Development (ISD) Week 3

Lesson 4: Who Goes There?

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

printf( Please enter another number: ); scanf( %d, &num2);

MITOCW watch?v=kz7jjltq9r4

Chapter Fourteen Bonus Lessons: Algorithms and Efficiency

Dynamic Programming. See p of the text


Example Program. public class ComputeArea {

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

SESSION 3 Programming Languages for Objects

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

BM214E Object Oriented Programming Lecture 8

Full file at

In this simple example, it is quite clear that there are exactly two strings that match the above grammar, namely: abc and abcc

CS 231 Data Structures and Algorithms, Fall 2016

System.out.print("Enter sales for salesperson " + i + ": "); sales[i] = scan.nextint();

13 th Windsor Regional Secondary School Computer Programming Competition

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

16-Dec-10. Consider the following method:

Eventually, you'll be returned to the AVD Manager. From there, you'll see your new device.

What two elements are usually present for calculating a total of a series of numbers?

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

Warm-Up: COMP Programming with Iterations 1

Contents. What's New. Dropbox / OneDrive / Google drive Warning! A couple quick reminders:

CS 2110 AEW SIGN UP FOR 1 CREDIT S/U COURSE!!!

Embedding Python in Your C Programs

Transcription:

The User Experience Java Programming 3 Lesson 2 User Friendly Coding In this lesson, we'll continue to work on our SalesReport application. We'll add features that make users happy by allowing them greater control over their experience! Always keep your users in mind when creating code. User Input: Command Line Let's try letting users enter initial start-up information at the command line. Although this technique is not as popular as it was pre-web or pre-gui days, it's still sometimes useful. Edit Main.java from your java3_lesson01 project as shown in blue: CODE TO TYPE: public class Main public static void main(string[] args) if (args[0]!= null) SalesReport mysalesinfo = new SalesReport(); mysalesinfo.testme(); Now, check out the changes we made: OBSERVE: Adding Command Line Arguments to Main public class Main public static void main(string[] args) if (args[0]!= null) SalesReport mysalesinfo = new SalesReport(); mysalesinfo.testme(); Some applications might not have a graphical user interface for user input, so Java requires that we declare the main method specifically with arguments, as public static void main(string[] args). Java uses the formal parameter String[] args to allow the user to provide input. The input provided is cast as a String and put into the array args. The main () method in the class called receives these arguments, so the programmer has to access the args array in order to get those values. Specifically, args[0] (in the main method) are made up of whatever is entered for the first argument. The argument variable type is String, so if the user enters 5 for the first argument, args[0] will be equal to the String "5" (not the numeral). In our example, we check to see if args[0] is null. We generally assume that if there are no command line arguments, the 0 element of the array is null. Save and run it.

The condition for the if is (args[0]!= null), so Java wants to go to args[0] immediately. But since no arguments were given, args[0] doesn't exist. In fact, even asking for args[0] is out of bounds. So, what should we do? Consider checking the length of args instead. Edit Main.java as shown in blue: CODE TO TYPE: public class Main public static void main(string[] args) if (args.length > 0) int argin = Integer.parseInt(args[0]); SalesReport mysalesinfo = new SalesReport(argIn); mysalesinfo.testme(); else SalesReport mysalesinfo = new SalesReport(); mysalesinfo.testme(); OBSERVE: Fixing Our Mistake public class Main public static void main(string[] args) if (args.length > 0) int argin = Integer.parseInt(args[0]); SalesReport mysalesinfo = new SalesReport(argIn); mysalesinfo.testme(); else SalesReport mysalesinfo = new SalesReport(); mysalesinfo.testme(); Now, we check to see if the length of the args array is greater than 0. If it is, we set the local variable argin to the int value of the args[0] element. Then we create a SalesReport object named mysalesinfo by passing argin to the constructor of the new SalesReport object. This will produce an error because we have not yet modified the SalesReport class. Next, we call the testme() method of the SalesReport class. If the args array length is not greater than 0, we create our SalesReport class just like we did earlier. Then we call the testme() method of the SalesReport class. Go ahead and save this program.

new SalesReport(argIn) is underlined in red, because we haven't defined this additional constructor with a parameter (SalesReport(int x)) yet. Java/Eclipse offers some suggestions, but ignore them for now. Here we give the user an opportunity to enter the number of salespeople before the code starts. If the user doesn't enter a number here, we'll prompt her to do so when the SalesReport class starts. If the user does input a number, we want to pass it to the SalesReport class through its Constructor, so we'll need a new Constructor with the formal parameter of an int before we can test our new Main. That's why you see Java's comment: Create constructor 'SalesReport(int)'. If the user doesn't enter a number, we'll use our recently edited Constructor to prompt for it. Overloading the Constructor Let's write that Constructor, this time one that receives the number of salespeople. In object-oriented terms, we'll overload the Constructor method. Overloading occurs when a Class has two methods with the same name, but different signatures (numbers and/or types of parameters). Edit SalesReport in your java3_lesson01 project, as shown in blue: CODE TO TYPE: SalesReport import java.util.scanner; public class SalesReport int salespeople; int sum; int sales[]; public SalesReport(int howmany) this.salespeople = howmany; public SalesReport() System.out.print("Enter the number of salespersons: "); this.salespeople = scan.nextint(); public void testme() getsalesinput(); providesalesoutput(); public void getsalesinput() System.out.print("Enter sales for salesperson " + i + ": "); sales[i] = scan.nextint(); public void providesalesoutput() System.out.println("\nSalesperson Sales"); System.out.println("--------------------"); sum = 0;

System.out.println(" " + i + " " + sales[i]); sum = sum + sales[i]; System.out.println("\nTotal sales: " + sum); Save it, then go back to the Main class. The error is gone now. Run the application in the Main class. Since we didn't run it with input from the command line, Java prompts us for this information. OBSERVE: Overloaded Constructor import java.util.scanner; public class SalesReport int salespeople; int sum; int sales[]; public SalesReport(int howmany) this.salespeople = howmany; public SalesReport() System.out.print("Enter the number of salespersons: "); this.salespeople = scan.nextint(); public void testme() getsalesinput(); providesalesoutput(); public void getsalesinput() System.out.print("Enter sales for salesperson " + i + ": "); sales[i] = scan.nextint(); public void providesalesoutput() System.out.println("\nSalesperson Sales"); System.out.println("--------------------"); sum = 0; System.out.println(" " + i + " " + sales[i]); sum = sum + sales[i]; System.out.println("\nTotal sales: " + sum);

This constructor lets us pass a parameter to the SalesReport class when we create it. The no-parameter constructor works as it did before, allowing the user to select the number of salespeople at run-time. A Closer Look at Main Take a look at the line we wrote in order to get the argument to pass to the SalesReport constructor, SalesReport(int howmany): OBSERVE: main args public class Main public static void main(string[] args) if (args.length > 0) int argin = Integer.parseInt(args[0]); SalesReport mysalesinfo = new SalesReport(argIn); mysalesinfo.testme(); else SalesReport mysalesinfo = new SalesReport(); mysalesinfo.testme(); If we allow the user to specify an argument initially, and (args.length > 0) is true, what parameter is passed to the constructor of SalesReport? The code for the Constructor seems reasonable. argin is declared as an int, so passing it meets the formal parameter requirements for the Constructor SalesReport(int howmany) in the SalesReport class. But what is this Integer.parseInt(args[0])? The variable in the main() is (String [ ] args), so everything given to the method as an args[] is cast to a String. And because Java would interpret, for example, 5 as String "5" and not an int, we need to make String into an int. Go to the API to see the java.lang.integer class, then consider the two snippets of code below (keep in mind that args[0] is a String): Integer.parseInt(args[0]) Integer.valueOf(args[0].intValue()) Ultimately, both techniques take a String argument and return an int value for it. Similar methods are used in other wrapper classes in java.lang (also called convenience classes) to parse from Strings to other primitive data types. These methods are particularly useful, because they allow Java to have a measure of conformity. Because Java interprets all input arguments as Strings, and is unable to anticipate user input, it needs some conformity. But as a programmer, you know what to expect as input, and can convert the passed String to any type you want. In our code, we have a constructor that receives an int, so we convert that passed String argument to an int. Entering Command Line Arguments So, how do we allow users to give arguments from the command line? Let's go over that from within Eclipse. (Later, we'll show you how to do it when you're not in Eclipse.) If you haven't done so already, save your Main class. Right-click it and select Run As Run Configurations:

Select the Arguments tab and enter 5 in the Program arguments: box. Then click Run:

There's a prompt in the Console to Enter sales for salesperson: for the number of times that you specified. Now users can enter any number of salespeople they want, and we won't have to edit the code. Users can provide their input either when they Run, or when prompted by the program. Sweet! Run the program again with Run As Java Application only. It still prompts for 5 salespeople. That's because we didn't remove the "5" argument. It's still set for 5 in Eclipse. Choose Run As Run Configurations, select the Arguments tab, and delete the number you set earlier. Now, Run As Java Application again; it should prompt you as expected. Code Flexibility Revisited

Fooling the User Being diplomatic managers, we don't want to identify anyone on our staff as "Salesperson 0," but 0 is assigned automatically as Java's ID for the first index in the array. Fortunately, programmers have power. While we can't change the way Java is written, we can change the way it looks to the user. And we can do it without going to all the trouble of manipulating programming structures, such as arrays. Instead, we can make Java print a higher number to the user. Salesperson 1 is still identified with the array element sales[0], but he doesn't need to know that! Let's give it a try. Edit SalesReport as shown in blue: CODE TO TYPE: import java.util.scanner; public class SalesReport int salespeople; int sum; int sales[]; public SalesReport(int howmany) this.salespeople = howmany; public SalesReport() System.out.print("Enter the number of salespersons: "); this.salespeople = scan.nextint(); public void testme() getsalesinput(); providesalesoutput(); public void getsalesinput() System.out.print("Enter sales for salesperson " + (i+1) + ": "); sales[i] = scan.nextint(); public void providesalesoutput() System.out.println("\nSalesperson Sales"); System.out.println("--------------------"); sum = 0; System.out.println(" " + (i+1) + " " + sales[i]); sum = sum + sales[i]; System.out.println("\nTotal sales: " + sum);

Save and run it (as a Java Application from Main). Tip It's often easier to change the appearance of our output by changing print statements than by changing programming structures. Finding the Max Suppose we want to find the maximum sales and the salesperson responsible for that number. We can do that! Edit SalesReport as shown in blue: CODE TO TYPE: import java.util.scanner; public class SalesReport int salespeople; int sum; int sales[]; public SalesReport(int howmany) this.salespeople = howmany; public SalesReport() System.out.print("Enter the number of salespersons: "); this.salespeople = scan.nextint(); public void testme() getsalesinput(); providesalesoutput(); findmax(); public void getsalesinput() System.out.print("Enter sales for salesperson " + (i+1) + ": "); sales[i] = scan.nextint(); public void providesalesoutput() System.out.println("\nSalesperson Sales"); System.out.println("--------------------"); sum = 0;

System.out.println(" " + (i+1) + " " + sales[i]); sum = sum + sales[i]; System.out.println("\nTotal sales: " + sum); public void findmax() int max = 0; if (max < sales[i]) max = sales[i]; System.out.println("\nMaximum sales is " + max); Save and run it (as a Java Application from Main). The program works fine, but who has the maximum (largest) sale? Edit SalesReport by changing the findmax () method as shown in blue: CODE TO TYPE: import java.util.scanner; public class SalesReport int salespeople; int sum; int sales[]; public SalesReport(int howmany) this.salespeople = howmany; public SalesReport() System.out.print("Enter the number of salespersons: "); this.salespeople = scan.nextint(); public void testme() getsalesinput(); providesalesoutput(); findmax(); public void getsalesinput() System.out.print("Enter sales for salesperson " + (i+1) + ": "); sales[i] = scan.nextint();

public void providesalesoutput() System.out.println("\nSalesperson Sales"); System.out.println("--------------------"); sum = 0; System.out.println(" " + (i+1) + " " + sales[i]); sum = sum + sales[i]; System.out.println("\nTotal sales: " + sum); public void findmax() int max = 0; if (max < sales[i]) max = sales[i]; System.out.println("\nSalesperson " + (i+1) + " had the highest sale with $" + max); The i is underlined and Java tells us it cannot be resolved. That's because i is declared in the loop initialization, so its scope is only within the loop. Not to worry, I'm confident we can fix this! Let's try moving i out of the loop. Edit SalesReport by changing the findmax() method as shown in blue: CODE TO TYPE: import java.util.scanner; public class SalesReport int salespeople; int sum; int sales[]; public SalesReport(int howmany) this.salespeople = howmany; public SalesReport() System.out.print("Enter the number of salespersons: "); this.salespeople = scan.nextint();

public void testme() getsalesinput(); providesalesoutput(); findmax(); public void getsalesinput() System.out.print("Enter sales for salesperson " + (i+1) + ": "); sales[i] = scan.nextint(); public void providesalesoutput() System.out.println("\nSalesperson Sales"); System.out.println("--------------------"); sum = 0; System.out.println(" " + (i+1) + " " + sales[i]); sum = sum + sales[i]; System.out.println("\nTotal sales: " + sum); public void findmax() int max = 0; int i; for (i=0; i < sales.length; i++) if (max < sales[i]) max = sales[i]; System.out.println("\nSalesperson " + (i+1) + " had the highest sale with $" + max ); Save and run it (as a Java Application from Main). Give it 3 salespeople with sales amounts of 3, 4, and 5: OBSERVE: Output from SalesReport Application Enter the number of salespersons: 3 Enter sales for salesperson 1: 3 Enter sales for salesperson 2: 4 Enter sales for salesperson 3: 5 Salesperson Sales --------------------

1 3 2 4 3 5 Total sales: 12 Salesperson 4 had the highest sales with $5 Hmm. That's better, but there's still a problem. Who is this Salesperson 4 and why are they outselling our other salespeople?! The real problem isn't where we declare the loop variable, but when the loop is done. The last loop iteration might not always be where max was set. When we come out of the loop, i will always be the last value Java saw in the loop. So, we need to remember who (which loop index) has the maximum sale by putting it in memory (that is, giving it a variable name and in doing so, a memory location). Edit SalesReport as shown in blue: CODE TO TYPE: import java.util.scanner; public class SalesReport int salespeople; int sum; int sales[]; public SalesReport(int howmany) this.salespeople = howmany; public SalesReport() System.out.print("Enter the number of salespersons: "); this.salespeople = scan.nextint(); public void testme() getsalesinput(); providesalesoutput(); findmax(); public void getsalesinput() System.out.print("Enter sales for salesperson " + (i+1) + ": "); sales[i] = scan.nextint(); public void providesalesoutput() System.out.println("\nSalesperson Sales");

System.out.println("--------------------"); sum = 0; System.out.println(" " + (i+1) + " " + sales[i]); sum = sum + sales[i]; System.out.println("\nTotal sales: " + sum); public void findmax() int max = 0; int who = 0; if (max < sales[i]) max = sales[i]; who = i; System.out.println("\nSalesperson " + (who+1) + " had the highest sale with $" + max ); Save and run it (as a Java Application from Main). We are still "fooling" the user by adding 1 to the index who. (Nobody wants to be salesperson 0, right?) Don't Let the User Fool You Now, let's suppose our salespeople are having a bad year and they all lost money. Run the program (from Main) and enter negative sales numbers for everyone. This gives 0 as the output for the max sold, even though nobody actually sold 0. Edit SalesReport as shown in blue: CODE TO TYPE: import java.util.scanner; public class SalesReport int salespeople; int sum; int sales[]; public SalesReport(int howmany) this.salespeople = howmany; public SalesReport() System.out.print("Enter the number of salespersons: "); this.salespeople = scan.nextint();

public void testme() getsalesinput(); providesalesoutput(); findmax(); public void getsalesinput() System.out.print("Enter sales for salesperson " + (i+1) + ": "); sales[i] = scan.nextint(); public void providesalesoutput() System.out.println("\nSalesperson Sales"); System.out.println("--------------------"); sum = 0; System.out.println(" " + (i+1) + " " + sales[i]); sum = sum + sales[i]; System.out.println("\nTotal sales: " + sum); public void findmax() int max = sales[0]; int who = 0; if (max < sales[i]) max = sales[i]; who = i; System.out.println("\nSalesperson " + (who+1) + " had the highest sale with $" + max ); Save and run it (as a Java Application from Main), again with all negative sales numbers. It's not really fair to blame the user for such weird numbers. Something like this could really happen. Java provides tools for its programmers to handle all kinds of errors. In later lessons, we'll look explicitly at Exception and Error classes that will assist us in dealing with strange input. What's in store? Now we're ready to look at other Java capabilities Java and get even cozier with the API. We'll start by digging into the top level of the API packages and from there, we'll explore each structure (including classes, interfaces,

exceptions, and enumerations). Soon, you'll embrace Java and the API will become your best friend! See you in the next lesson... O'Reilly Media, Inc. - 1998-2012. All rights reserved.