Title: CS/IS 112
1CS/IS 112 Week 2
- Logic Problem
- More Java background and basics
- Values Variables, and operations
2- Easy Logic Problem 1a
-
- Solving computer programming assignments involves
paying attention to details - And figuring out your own best tools for solving
detailed problems -
- Claude has a facility for learning languages. In
each of the last four years (2002 through 2005),
he has set himself the task of learning a
different language so that, by the end of the
year, he could vacation in a country where the
language is spoken and make himself understood.
He learned each language through a different
method (in one case, through conversation with a
native speaker). Discover the year in which
Claude learned each language and the method he
utilized. -
- 1. Claude learned Spanish (which he did not
pick up by listening to audio tapes) during an
odd-numbered year. - 2. He learned Finnish in 2003
- 3. He learned Korean by watching vides.
- 4. Polish (which Claude did not learn in 2004)
is not the language that he learned by reading
books or listening to tapes. -
- YEAR Language Method
-
- 2002 __________ _________________________
- 2003 __________ _________________________
- 2004 __________ _________________________
- 2005 __________ _________________________
-
3 S F K P Bk Con Tp Vi
2002 2003 2004 2005 Books Conv Tapes Videos
4Classes and Objects
- CLASS A single unit that defines both the data
that will be used and the operations that can be
performed on the data - Operations in the above definition are formally
referred to as METHODS (informally they are
procedures and functions - OBJECT is a specific item in a class
- Characteristics of an object are its ATTRIBUTES
5Figure 1.8 The Traditional Translation Process
6Figure 1.9Compiling and Executing a Java Program
7A Well-Designed ProgramIs Built Using Modules
8Java identifiers
- Identifiers are just programmer defined names for
things and names are case sensitive at all times
in Java - Rules
- First character cannot be a digit
- Can contain letters, digits, underscore_ and
dollar signs - Cannot be a Java keyword
- Shoulds For this class MUSTs (conventions)
- We will use the book conventions
- Identifiers should be mnemonic
- All identifiers except for a class name should
begin with a lowercase character - First letter of each following word in the
identifier should be capitalized - First letter of class names should be capitalized
9Java Programming Basics
- // Everything to the right of the double slash is
a human comment - / and / Large block comment delimiters
- public class classname (classname must match your
sourcecode file name) - braces mark the beginning and end of a class
10More Java basics
- public static void main (String args) Every
application (not applet) must have a method name
main. Every class must contain at least 1 method - Braces also mark the beginning and end of
each method - System.out.print(Hello World!) Methods
contain statements and each statement is
terminated by a
11print and println methods
- System.out.print(Hello World!)
- General syntax
- objectname.print(data)
- General Java Class called System
- out is a specific object within that class
- Referred to as Standard Output Stream
- On most systems this is the video monitor
- The print method leaves the cursor right after
the last character - The println method always moves the cursor to the
beginning of the next line
12Hello World Basic Java
//File DisplayHelloWorld.java //Description
Displays Hello World! //Programmer Bruce
Haft //Date 2/27/2006 public class
DisplayHelloWorld public static void
main(String args) System.out.print(He
llo World!) // end of main() method
13Figure 1.16b showMessageDialog() Dialog
BoxQUESTION_MESSAGE
14Hello World Dialog Box
- //File DisplayADialog.java
- //Description Construction of a dialog
- //Programmer Bruce Haft
- //Date 2/27/2006
- import javax.swing.
- public class DisplayADialog
-
- public static void main(String args)
-
- JOptionPane.showMessageDialog(null,"Hello
World!", - "Sample",JOptionPane.WARNING_MESSAGE)
-
- System.exit(0)
- // end of main() method
-
15Data Types
- Primitive Data
- Operations on primitive data type are provided by
arithmetic symbols - Reference Data
- Operations provided as methods
16Figure 2.1 Primitive Data Types
17Integer data types
- Type Storage Range
- byte 1 byte -128 to 127
- short 2 bytes -32,768 to 32,767
- int (def) 4 bytes -2,147,483,648 to
- 2,147,483,647
- long 8 bytes -9,223,372,036,854,775,808
- to 9,223,372,036,865,775,807
18Floating Point
- Type Storage Range of Values
- float 4 bytes 1.40129846432481707e-45 to
- 3.40282346638528860e38
- double(def) 8 bytes 4.94065645841246544e-324 to
- 1.79769313486231570e308
- Range of values
- Precision of values
- Exponential notation
19Figure 2.2 Reference Types
20Characters
- Stored in 16-bit unsigned values Unicode
- Java provides a class named String for
manipulating this type of data. - String type is a reference type
- Most operations for strings will be methods
rather that arithmetic symbols
21Figure 2.3 The Letters JEANS Stored bya Java
Program
22Escape Sequences
- The backslash (\) character causes Java to
interpret the character that follows differently - \b move back one space
- \f move to the next page
- \n move to the next line
- \r carriage return
- \t move to the next tab setting
- \\ backslash character output
- \ single quote output
- \ double quote output
23Boolean Constant
- A type of data that is restricted to one of two
values - True
- False
- Cover more when we talk about decisions in
Chapter 4
24Figure 2.8 An Example of a Value Stored in a
Reference Variable
25Figure 2.9a Creating a Reference Variable
26Figure 2.9b Instantiating an Object
27Figure 2.10 The Location of Different Strings
Using the Same Reference Variable
28LoanCalculator.java
- / File LoanCalculator.java
- Sample loan calculation program written in plain
Java - by Bruce Haft ltltlt for Extra credit submission
PUT YOUR NAME HERE - February 20, 2006 ltltlt change the date to the
last date you made a change - /
- import java.text. //needed for formatting
- import java.io. //needed to access input stream
classes - public class LoanCalculator
-
29More LoanCalculator.java
- public static void main (String args)
- throws java.io.IOException
-
- String s1, s2, s3
- double interestRate, loanAmount, interest,
payment, - numerator, denominator
- int numYears
- int paymentsPerYear 12
- DecimalFormat num new DecimalFormat(",.00")
-
- // needed for conversion
- InputStreamReader isr new InputStreamReader(Sys
tem.in) - // needed to use readLine()
- BufferedReader br new BufferedReader(isr)
30More LoanCalculator.java
- System.out.print("Enter loan amount ")
- s1 br.readLine()
- loanAmount Double.parseDouble(s1)
- System.out.print("Enter interest rate in
decimal ") - s2 br.readLine()
- interestRate Double.parseDouble(s2)
- System.out.print("Enter number of years for the
loan ") - s3 br.readLine()
- numYears Integer.parseInt(s3)
31Next Week
- Finish Chapter 2
- Start Chapter 3
- All Extra Credit due by 3/13
32Last Loancalculator.java
- numerator ( loanAmount ( interestRate /
paymentsPerYear)) - denominator 1 - Math.pow((1 (interestRate /
paymentsPerYear)), - (-paymentsPerYear numYears))
- payment numerator / denominator
- interest ( paymentsPerYear payment numYears
) -loanAmount - System.out.print("\n\nThe monthly payment on the
loan would be " - num.format(payment))
- System.out.print("\n\nThe total interest on a
loan of " - num.format(loanAmount) " for " numYears
" years\n") - System.out.print("at a rate of " num.format(100
- interestRate) " would be "
num.format(interest)) -
-