Download presentation
Presentation is loading. Please wait.
Published byJoy Shepherd Modified over 9 years ago
1
CS/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. YEARLanguageMethod 2002___________________________________ 2003___________________________________ 2004___________________________________ 2005___________________________________
3
S F K P Bk Con Tp Vi 2002 2003 2004 2005 Books Conv Tapes Videos
4
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
5
Figure 1.8: The Traditional Translation Process
6
Figure 1.9: Compiling and Executing a Java Program
7
A Well-Designed Program Is Built Using Modules
8
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
9
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
10
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 ‘;’
11
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
12
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 }
13
Figure 1.16b: showMessageDialog() Dialog Box: QUESTION_MESSAGE
14
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 }
15
Data Types Primitive Data Operations on primitive data type are provided by arithmetic symbols Reference Data Operations provided as methods
16
Figure 2.1: Primitive Data Types
17
Integer data types TypeStorageRange byte1 byte-128 to 127 short2 bytes-32,768 to 32,767 int(def)4 bytes-2,147,483,648 to 2,147,483,647 long8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,865,775,807
18
Floating Point TypeStorageRange of Values float4 bytes1.40129846432481707e-45 to 3.40282346638528860e+38 double(def)8 bytes4.94065645841246544e-324 to 1.79769313486231570e+308 Range of values Precision of values Exponential notation
19
Figure 2.2: Reference Types
20
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
21
Figure 2.3: The Letters JEANS Stored by a Java Program
22
Escape Sequences The backslash (\) character causes Java to interpret the character that follows differently –\bmove back one space –\fmove to the next page –\nmove to the next line –\rcarriage return –\tmove to the next tab setting –\\backslash character output –\’single quote output –\”double quote output
23
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
24
Figure 2.8: An Example of a Value Stored in a Reference Variable
25
Figure 2.9a: Creating a Reference Variable
26
Figure 2.9b: Instantiating an Object
27
Figure 2.10: The Location of Different Strings Using the Same Reference Variable
28
LoanCalculator.java /* File: LoanCalculator.java Sample loan calculation program written in plain Java by: Bruce Haft <<< for Extra credit submission PUT YOUR NAME HERE February 20, 2006 <<< 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 {
29
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);
30
More 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);
31
Next Week Finish Chapter 2 Start Chapter 3 All Extra Credit due by 3/13
32
Last 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)); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.