cs/is 112 – week 2 logic problem more java background and basics values variables, and operations
TRANSCRIPT
![Page 1: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/1.jpg)
CS/IS 112 – Week 2
• Logic Problem
• More Java background and basics
• Values Variables, and operations
![Page 2: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/2.jpg)
Easy Logic Problem 1a
Solving computer programming assignments involves paying attention to detailsAnd 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 20033. 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 __________ _________________________
![Page 3: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/3.jpg)
S F K P Bk Con Tp Vi
2002
2003
2004
2005
Books
Conv
Tapes
Videos
![Page 4: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/4.jpg)
Classes 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
![Page 5: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/5.jpg)
Figure 1.8: The Traditional Translation Process
![Page 6: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/6.jpg)
Figure 1.9:
Compiling and Executing a
Java Program
![Page 7: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/7.jpg)
A Well-Designed ProgramIs Built Using Modules
![Page 8: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/8.jpg)
Java 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
![Page 9: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/9.jpg)
Java 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
![Page 10: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/10.jpg)
More 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 ‘;’
![Page 11: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/11.jpg)
print 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
![Page 12: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/12.jpg)
Hello 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(“Hello World!”);
} // end of main() method
}
![Page 13: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/13.jpg)
Figure 1.16b: showMessageDialog() Dialog Box:
QUESTION_MESSAGE
![Page 14: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/14.jpg)
Hello 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}
![Page 15: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/15.jpg)
Data Types
•Primitive Data
•Operations on primitive data type are provided by arithmetic symbols
•Reference Data
•Operations provided as methods
![Page 16: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/16.jpg)
Figure 2.1: Primitive Data Types
![Page 17: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/17.jpg)
Integer 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
![Page 18: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/18.jpg)
Floating Point
Type Storage Range of Values
float 4 bytes 1.40129846432481707e-45 to
3.40282346638528860e+38
double(def) 8 bytes 4.94065645841246544e-324 to
1.79769313486231570e+308
Range of values
Precision of values
Exponential notation
![Page 19: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/19.jpg)
Figure 2.2: Reference Types
![Page 20: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/20.jpg)
Characters
• 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
![Page 21: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/21.jpg)
Figure 2.3: The Letters JEANS Stored by
a Java Program
![Page 22: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/22.jpg)
Escape 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
![Page 23: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/23.jpg)
Boolean 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
![Page 24: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/24.jpg)
Figure 2.8: An Example of a Value Stored in a Reference
Variable
![Page 25: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/25.jpg)
Figure 2.9a: Creating a Reference Variable
![Page 26: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/26.jpg)
Figure 2.9b: Instantiating an Object
![Page 27: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/27.jpg)
Figure 2.10: The Location of Different Strings Using the Same
Reference Variable
![Page 28: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/28.jpg)
LoanCalculator.java/* File: LoanCalculator.java
Sample loan calculation program written in plain Javaby: Bruce Haft <<< for Extra credit submission PUT YOUR NAME HEREFebruary 20, 2006 <<< change the date to the last date you made a change
*/
import java.text.*; //needed for formattingimport java.io.*; //needed to access input stream
classespublic class LoanCalculator
{
![Page 29: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/29.jpg)
More 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(System.in);
// needed to use readLine()
BufferedReader br = new BufferedReader(isr);
![Page 30: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/30.jpg)
More LoanCalculator.javaSystem.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);
![Page 31: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/31.jpg)
Next Week
• Finish Chapter 2
• Start Chapter 3
• All Extra Credit due by 3/13
![Page 32: CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations](https://reader035.vdocuments.us/reader035/viewer/2022081519/56649e025503460f94aed90a/html5/thumbnails/32.jpg)
Last Loancalculator.javanumerator =( 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));
}
}