BIT115: Introduction to Programming

Download Report

Transcript BIT115: Introduction to Programming

Lecture 5a
Instructor: Craig Duckett
Assignments 1, 2
Assignment 1 Graded! Returned! Woot!
Assignment 2 Due Lecture 7 by midnight
Monday, July 27th
Assignment 1 Revision due Lecture 8 by midnight
Wednesday, July 29th
Assignment 2 Revision Due Lecture 10 by midnight
Wednesday, August 5th
Assignment Announcements
• Assignment 1 GRADED! RETURNED! WOOT!
• Assignment 2 DUE Lecture 7, Monday, July 27th, by midnight
• Assignment 1 Revision DUE Lecture 8, Wednesday, July 29th, by midnight
• Assignment 2 Revision DUE Lecture 10, Wednesday, August 5th, by
midnight
• Assignment 3 DUE Lecture 11, Monday, August 10th, by midnight
• Assignment 3 Revision DUE Lecture 13, Monday, August 17th, by midnight
• Assignment 4 DUE Lecture 15, Monday, August 24th, by midnight
NO REVISION AVAILABLE
• Extra Credit 01 DUE Lecture 15, Wednesday, August 26th, by midnight
3
Lecture 5a Announcements
• Assignment 1 GRADED and RETURNED!
• Solutions Posted
• Assignment 2
• Assignment 2 Hints
• Mid-Term
• Mid-Term Study Guide Posted
• Output
• System.out
• "string"
• Println and print
• Java Escape Sequences ("Special Characters")
• The + Operator
• Addition
• String Concatenation
• Input
• Scanner class and (System.in)
• nextInt() method
• hasNextInt() method
• nextLine() method
MID-TERM
Mid-Term is Lecture 8 ( Wednesday, July 29th )
• It will cover everything learned through Lecture 6
• Please be prompt, and bring a pencil … don’t worry, I’ll supply the paper
Mid-Term
Mid-Term is scheduled for LECTURE 8, Wednesday, July 29th
• It will cover everything learned up through Lecture 6
• It is scheduled for the entire session, so you will have enough more than
enough time to work through each of the questions, however most students
finish in about 90 minutes.
• When you are finished, you can hand it in and you are done for the day, so feel
free to go home or the nearest pub for a celebratory pint ;-)
• It will be done entirely with pencil-and-paper (no .java files).
Mid-Term Study Guide is available on the main page of BIT115
website (located in the right-hand column)
Mid-Term, continued…
Mid-Term is scheduled for LECTURE 8
Wednesday, July 29th
The Mid-Term Exam will focus on three learning outcomes:
• conceptualize the logical steps needed to accomplish a task,
• apply structured programming techniques to accomplish a task,
• test and debug a program
Exam Topics:
• Setting up a city with walls, things, robots
• Using the robots built-in services
• Extending a robot to include new services
• Tracing code and pinpointing where it goes wrong
• Explaining the compile/run process
• Selecting when to use different programming structures like loops, decisions,
and services
• Writing syntax for loops, decisions, local variables, and parameters
Again, the exam will be similar to the quiz format (i.e., pencil-paper, from memory,
individual work).
And Now
… The Quiz
Assignment 2 Part 3 - Hints
Here are some helpful hints on how to approach “solving” Assignment 2 Part 3…
1. Create a class that extends Robot and a build a constructor. Remember if you do it all in one class
(the same class that contains main, then the file name, the class name, and the constructor name
all have to be the same.
For example:
public class A2_Part_3 extends Robot
{
public A2_Part_3(City theCity, int street, int avenue, Direction aDirection, int item)
{
super(theCity, street, avenue, aDirection, item);
}
2. When you create a new instance of the robot down in main, then it too has to have this
same name ( and not Robot).
For example:
A2_Part_3 rob = new A2_Part_3(wallville, 1, 2, Direction.EAST, 0);
3. Create a turnRight() and turnAround() method, which you will use along with three
other methods (which I'll talk about one at a time).
4. Create a movetoWall() method which moves while the front is clear
5. Create a doEverything() method which will do the following:
• Initialize a counter to 0
• Use the counter in a while loop four times
• Call movetoWall() method
• Pick up a Thing using the pertinent method
• Call the turnAround() method
• Call moveToWall() method
• Turn left
• Move
• Turn left
• Increment the counter by 1
//second part of doEverything continued on next slide
6. [doEverything continued] Next, use a while loop to count things in the backpack using
the pertinent method, and as long as the count is greater than zero, move and put a
thing down.
// third part of doEverything continued on next slide
7. [doEverything continued] Finally, call a returnToStart() method to go back to where the
robot start [end of doEverything]
8. The returnToStart() method will do the following:
• turn around
• Call moveToWall() method
• Turn right
• Call moveToWall() method
• Turn right
Chapter 6.6.1: Output
(Printing Text Messages To The Screen)
•
Info we get from the user is input
•
•
•
Either from the keyboard, mouse, or something else like a
network connection or other type of cable
We will be looking at input in the next Lecture
Info we show to the user is output
•
•
This can be a message, additional information, even a request
that the user perform some kinf of action
These messages and information are typically “hard-coded” into
the program itself, then pushed out to the screen, console, or
monitor using specific print-to-the-screen mechanisms
(services) that are a part of Java
The Console Window
 The console window that starts a Java application is typically





known as the standard output device.
The standard input device is typically the keyboard.
Java sends information to the standard output device by using a
Java class stored in the standard Java library.
In jGRASP, this output is displayed as part of Compile Messages
Java classes in the standard Java library are accessed using the Java
Applications Programming Interface (API).
The standard Java library is commonly referred to as the Java API.
“Hello World!”
The Console Window
 Example:
System.out.println("Programming is great fun!");
 This line uses the System class from the standard Java library.
 The System class contains methods and objects that perform system level tasks.
 The out object, a member of the System class, contains the methods print and
println.
The Console Window
 The print and println methods actually perform the task of sending characters to the output
device.
 The line:
System.out.println("Programming is great fun!");
is pronounced: System dot out dot printline
 The value inside the parenthesis will be sent to the output device (in this case, a string).
 The println method places a newline character at the end of whatever is being printed out.
 The following lines would be printed out on separate lines since the first statement sends a
newline command to the screen:
System.out.println("This is being printed out");
System.out.println("on two separate lines.");
The Console Window
 The print statement works very similarly to the println statement.
 However, the print statement does not put a newline character at the end of
the output.
 The lines:
System.out.print("These lines will be");
System.out.print("printed on");
System.out.println("the same line.");
Will output:
These lines will beprinted onthe same line.
Notice the odd spacing? Why do some words run together?
The Console Window
 For all of the previous examples, we have been printing out strings of
characters.
 Later, we will see that much more can be printed.
 There are some special characters that can be put into the output.
System.out.print("This line will have a newline at the end.\n");
System.out.print("This doesn’t show up on the same line.");
 The \n in the string is an escape sequence that represents the newline
character.
 Escape sequences allow the programmer to print characters that
otherwise would be unprintable.
Java Escape Sequences ("Special Characters")
\n
newline
Advances the cursor to the next line for subsequent printing
\t
tab
Causes the cursor to skip over to the next tab stop
\b
backspace
Causes the cursor to back up, or move left, one position
\r
carriage return
Causes the cursor to go to the beginning of the current line, not
the next line
\\
backslash
Causes a backslash to be printed
\'
single quote
Causes a single quotation mark to be printed
\"
double quote
Causes a double quotation mark to be printed
Java Escape Sequences ("Special Characters")
 Even though the escape sequences are comprised of two characters, they are
treated by the compiler as a single character.
System.out.print("These are our top sellers:\n");
System.out.print(“==========================\n");
System.out.print("\t- Computer games\n\t- Coffee\n ");
System.out.println("\t- Aspirin");
Would result in the following output:
These are our top sellers:
==========================
- Computer games
- Coffee
- Asprin
 With these escape sequences, complex text output can be achieved.
The + Operator
 The + operator can be used in two ways.
 (1) as a concatenation operator
 (2) as an addition operator
 If either side of the + operator is a string, the result will be a string.
System.out.println("Hello " + "World!");
Hello World!
System.out.println("The value is: " + 5);
The value is: 5
System.out.println("The value is: " + value);
The value is: <whatever value is>
System.out.println("The value is" + ": \n" + 5);
The value is:
5
String Concatenation
• Java commands that have string literals must be treated with care.
• A string literal value cannot span lines in a Java source code file.
System.out.println("This line is too long and now it
has spanned more than one line, which will cause a
syntax error to be generated by the compiler. ");
• The String concatenation operator can be used to fix this problem.
System.out.println("These lines are " +
"now ok and will not " +
"cause the error as before.");
• String concatenation can join various data types.
System.out.println("We can join a string to " +
"a number like this: " + 5);
String Concatenation
 The concatenation operator can be used to format complex String
objects.
System.out.println("The following will be printed " +
"in a tabbed format: " +
"\n\tFirst = " + 5 * 6 + ", " +
"\n\tSecond = " + (6 + 4) + "," +
"\n\tThird = " + 16.7 + ".");
 Notice that if an addition operation is also needed, it must be put in
parenthesis.
Printing as Part of Program Decision-Making
int newNum = 3;
if( newNum >= 5 )
{
System.out.println(
newNum + “ is larger than 4”);
}
else
{
System.out.println(
newNum + “ is smaller than 5.”);
}
Two Points:
1) ‘+’ for concatenation
2) spaces in “ ”double-quotes
Demo: Basic Output Demo.java
LECTURE 5:
ICE 1.1 and 1.2
30