|
Class 10 — February 8, 2012
|
ExamplesData |
Agenda
Reading
|
Problem solving
- The purpose of our tackling today TwoLinesFromFile.java is to hone your skills on string manipulation
- Much of the data people need to consider when problem solving is text. For example, the text may specify an input source or have important information embedded within it.
- An added benefit of developing the program is that it will help us prepare for a future assignment.
-
Program
TwoLinesFromFile.java
expects to be given an input source. That input source will
contain two lines. Both lines provide some necessary
information. All of the code for getting the two lines
are supplied for you. I did so, because this subtask is
not the interesting part.
// set up input scanner Scanner stdin = new Scanner( System.in ); // get name of file to be processed System.out.print( "What is your file: " ); String s = stdin.next(); // get a representation of the file File f = new File( s ); // get connection to the file Scanner reader = new Scanner( f ); // get data from the file String line1 = reader.nextLine(); String line2 = reader.nextLine();
- The first of the two input lines contains the name of a JPG image; the second line contains a forecast.
- On the first line, the start of the name of the JPG can be found immediately after the first occurrence of the string "images/".
- On that same line, the end of the name of the JPG can be found immediately before the first double quote following the start of the name of the image.
- On the second line, the start of the forecast can be found immediately after the first occurrence of the string "</b>".
- On that same line, the end of the forecast can be found immediately before the first first occurrence of "<br>" following the start of forecast.
- By storing the indices of the beginning and end of the image name, and
by also storing the indices of the beginning and end of the forecast,
substrings of the two lines can be gotten for acquiring the image name
and forecast.
// ******* get the indices for getting the jpg name int i1 = 0; int i2 = 0; // ******* get the substring giving the name of the jpg String jpg = ""; // ******* get the indices for getting the forecast int i3 = 0; int i4 = 0; // ******* get the substring giving the forecast String forecast = "";
- Here are four files to download to use for testing your effort.
|
Class 9 — February 6, 2012
|
Examples |
Agenda
Reading
|
Survey
- Take the super power survey. The survey will create a web page. If the indicated e-mail id was mst3k and the indicated power was Dinosaur metamorphosis, then web page
http://www.cs.virginia.edu/cs1112/people/mst3k/power.txt
would be created.
Just the facts
- There are no String methods to change an existing String. There are many methods to produce new Strings.
- 'x' and "x" are different. The former is a char; the latter is a String.
- null and "" are different. The former indicates a non-reference; the latter is a String with length 0.
- A character string within double quotes is a String literal; e.g., "starting".
- "name" and name are different. The former is a String literal; the latter is an identifier.
- The characters in a String are accessible by their index. The first character has index 0, the second character has index 1, and so on.
- Suppose
- s, t, and u are strings,
- m and n are integers.
- s.length() is the number of characters in the character string represented by s.
- s.charAt( m ) is a the char value of the character in s at index m.
- s.trim() returns a new copy of character string s with any leading or trailing whitespace removed.
- s.toLowerCase() returns a new copy of the character string s with any uppercase characters replaced by their lowercase counterparts.
- s.toUpperCase() returns a new copy of the character string s with any lowercase characters replaced by their uppercase counterparts.
- s.indexOf( t, m) returns the int value equal to the index of the first occurrence of t's character string within s's character string when starting the search at index m. If there is no occurrence then the value of the method is -1.
- s.lastIndexOf( t, m ) returns the int value equal to the index of the last occurrence of t's character string within s's character string that starts on or before index m. If there is no occurrence then the value of the method is -1.
- s.substring( m, n ) returns a new string representing the substring of s starting with its character at index m and continuing to the character with index n-1.
- s.replace( t, u ) returns a new string formed by first copying all of the characters of s and then replacing all occurrences of substring t with string u.
- s.replaceFirst( t, u ) returns a new string formed by first copying all of the characters of s and then replacing its first occurrence of the string pattern indicated by t with string u.
- s.replaceAll( t, u ) returns a new string formed by first copying all of the characters of s and then replacing all occurrences of the string pattern indicated by t with string u.
- s.compareTo( t ) returns an integer value indicating the lexicographical (alphabetical) relationship between s and t:
- Returns 0 when s's character sequence is the same as t's character sequence;
- Returns a negative value when s's character sequence occurs lexicographically before t's character sequence;
- Returns a positive value when s's character sequence occurs lexicographically after t's character sequence;
- s.equals( t ) returns a boolean value indicating whether s and t represent the same character sequence (i.e., do they look alike).
- s.substring( m ) returns a new string representing the substring of s starting with its character at index m and continuing to the end of s.
- s.indexOf( t ) is equivalent to s.indexOf( t, 0 ).
- s.lastIndexOf( t ) is equivalent to s.lastIndexOf( t, m ), where m+1 is the length of character string represented by s.
|
Class 8 — February 3, 2012
|
Examples
Reading
|
Agenda
|
|
|
Class 7 — February 1, 2012
|
Examples
Reading
|
Agenda
|
Objectify
- Our initial exploration of objects will concentrate using some of classes from
java.util. java.awt, and javax.swing. The capabilities of these libraries allow
for
- Popup windows
- Graphical elements supporting the drawing and displaying of shapes, texts, and images;
- Random number generators;
- File and URL support for accessing and writing image and text files;
- Dictionaries and lists for data storage and manipulation.
- When we need a new object, the first thing to do is to tell Java how to construct it. Afterwards, we can manipulate the object by sending it messages. A Java library designer must specify both the attributes its objects will have and which messages those objects can process.
- Some messages to objects include information to assist the object in taking the right action. The information comes as parameters. Parameters are passed to a message through a parenthesized list following the message name. If there is more than one parameter, they are separated by commas.
- Java requires a library designer to specify the types of and the order that parameters are to be given in a parameter list. To send a particular message, there must be a properly fill out parameter list. So be careful when sending a message to object that you know what and how it wants information to be passed.
- Some objects send replies back for some of the messages they receive. Java requires a library designer to specify the type of reply (if any) a message will generate. In response to getting a message, an object will always reply back with a value of the type indicated by its designer. The reply is known as the message's return value.
Some important APIs' official documentation
Some important APIs' unofficial documentation
|
|
Class 6 — January 30, 2012
|
Examples
Readings
|
Agenda
Ponder
Remember
Check out
|
Wrap up
Each of the programs we considered today offered something for future problem solving
- WhatsUp.java
- Highlighted the differences in the evaluation of mathematical and programming expressions
- CelsiusToFahrentheit.java
- Demonstrated how with parentheses and operator reordering we can achieve proper evaluation
- ReadValues.java
- Demonstrated different ways to read input values
- TellMe.java
- Highlighted the importance of naming values for readability
- MP3Sizer.java
- Demonstrated how naming of constants assists readability and understanding
- Relations.java
- Highlighted the use of relational and equality operators in expression evaluation
- PiggyBank.java
- Demonstrated a standard convention for breaking up a long expression over multiple lines
|
|
Class 5 — January 27, 2012
|
ExamplesReadings
|
AgendaGeneral problem solving requires two-way communication; that is, getting data and sending information back. Java provides class Scanner as a means for communicating with an input source. Scanner is very flexible with regard to input sources. Files, web pages, and user input are all acceptable. We will explore today how to create and use Scanners that read user-supplied numerical inputs. A very basic way of getting input is to directly call for the user to supply it. Java provides programmers with object System.in as the input counterpart to System.out; that is, System.in is the Java representation of a user's keyboard. When System.in is specified to be the input source for a Scanner, a program can use the Scanner to process user input. |
Your need to know
- Scanner is part of the java.util standard library.
- An import statement indicates that the software needs to make use of a library.
- Import statements occur immediately before the class definition.
- The statement
provides access to the entire java.util library.import java.util.*;
- Scanner is a class, but it is not a program. What we can get from it are Scanner objects.
- A Scanner object can be directed to get information from an input source.
- Java supplies the new operator to get a new object.
- The act of setting up a new object is called construction.
- The method that performs setting up is called a constructor.
- A Scanner constructor can be given a parameter to specify the input source.
- System.in is an input source associated with the keyboard.
- The expression
produces and constructs a new Scanner object that can read what the user enters.new Scanner( System.in )
- The statement
defines and initializes a Scanner variable named stdin. With the variable, a program can send messages to its object to read the next number, string, line of text, or logical value.Scanner stdin = new Scanner( System.in )
- Every Scanner object has a method nextInt() for reading the next input as an integer value.
- Every Scanner object has a method nextDouble() for reading the next input as a decimal value.
|
|
Class 4 — January 25, 2012
|
Examples
Readings
|
AgendaSophisticated problem solving requires the ability to abstract. A very basic part of abstraction is ability to name a value and to manipulate the value through a referencing of its name.
|
For inquiring minds
- Because any computer has a finite size, it is not possible for a computer to represent any possible value.
- The creators of Java specified under normal circumstances that 32-bits of memory are used for storing integer values and that 64 bits of memory are used for storing decimal values.
- As a bit has two possible values: 0 or 1, all numbers are represented internally as binary numbers. In fact all values are represented as binary numbers.
- In representing a number Java reserves one bit for keeping track of the sign of the number. Therefore, for integers there are 31 bits for the number itself. The biggest 31-bit binary number is 1111111111111111111111111111111. That binary number equals the decimal number 232 - 1 = 2,147,483,647. The smallest 31-bit number is -2,147,483,648.
- When doing calculations, the result can become too big or small. For example, 1000 * 1000 * 1000 * 1000 is one trillion, which is too big for 31 bits (computer scientists call that overflow). The 32-bit pattern produced doing the four multiplications of 1000 corresponds to -727379968.
-
FYI, the encoding for decimal numbers uses 52 bits for the number and 11 bits for the exponent. The biggest decimal number that can be represented is 21023 and the smallest positive nonzero value is 2-1074.
(Source Wikipedia) - In Java, a 32-bit integer is called an int value; a 64-bit decimal
number is called a
double value. int and double are types of values.
- Java provides a logical type called boolean and a character type called char.
- The char type is considered a numeric type.
- The numeric and logical types are the Java primitive types. The other types of values in Java are object types.
- An example of an object type is the String type.
- Java provides addition (+), subtraction (-), multiplication (*) , division (/), and remainder (%) operators for its numeric types.
- An int operation produces an int value; a double operation produces a double value; an operation with int and double values produces a double value.
- Java provides the concatenation operator (+) for strings. The concatenation operator produces a new string by combining its operands.
- Whenever one of the operands for a + operator is a string, it does concatenation; otherwise, it does numeric addition.
- There are two possible boolean values: true and false. Java provides an and operator (&&), an or operator (||), and a not operator (!) for manipulating boolean values.
- The && operator is true, when both of its operands are true; otherwise, the && operator is false.
- The || operator is true, when at least one of its operands is true; otherwise, the || operator is false.
- The ! operator produces the complement of its operand; i.e., ! true is false and ! false is true.
- The argument to a println() or print() methods can be any Java expression that produces a value.
|
|
Class 3 — January 23, 2012
|
Examples
Readings
|
AgendaLast class we handled a very important task in computational problem solving – basic user communication. This class we are going to
Linkify |
![]() |
Class 2 — January 20, 2012 |
Examples
Readings
|
Agenda
|
![]() |
Class 1 — January 18, 2012 |
Agenda
- Convince everyone to become a computing major
- Appreciate the wisdom of the movie What about Bob?
- Never, never, never click on this link!
- Meet course personnel
- Discuss syllabus and course mechanics
- Ensure a Java SDK is installed on all Windows laptops
- Assign readings and homework
- Say good afternoon the first time I say wahoo
- Download PrintWord.java and submit it
- I haiku do you
![]() |
Early January 2012 |
Practicing structured communication
Write an original, thoughtful haiku and electronically upload it using the class submission system by Noon Tuesday January 17 (the day before the start of classes).
In terms of structure a haiku is a three-line poem with the first line being five syllables, the second seven syllables, and the third line being five syllables. A haiku is not expected to rhyme. A haiku is expected to paint an image regarding feelings and experiences.
Your haiku is to be about your class expectations or concerns. The poem should be appropriate for other class members to read. Students who do not complete this assignment on time are subject to dismissal from the class.
Software development
A major activity occurring throughout the semester is developing the ability to program in the Java language. In order for this to happen you need two pieces of software on your personal computer.
- Java SDK (software development kit): the SDK will handle translating your instructions to computer code.
- DrJava: a program development environment.
SDK
If you are a Windows user, you need to download and install the Java SDK. The SDK is a free download available from Oracle. There are two versions depending whether your laptop is running 32-bit or 64-bit windows. Most recent laptops are 64-bit. If you are unsure the 32-bit version works for all.
Mac OS X users have the JDK software pre-installed, so nothing needs to be done with regard to the SDK.
DrJava
After taking care of making sure you have a Java JDK installed, you can download and install DrJava.
- Windows DrJava application
- Mac DrJava application
Readings
A handbook will be available from the bookstore. The handbook will contain an in-progress textbook and the major course examples. Please bring the handbook to every class meeting.




