* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download CS/IS 112 – Week 2 - Glendale Community College
Survey
Document related concepts
String literal wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Abstraction (computer science) wikipedia , lookup
Corecursion wikipedia , lookup
Class (computer programming) wikipedia , lookup
Java syntax wikipedia , lookup
Scala (programming language) wikipedia , lookup
Covariance and contravariance (computer science) wikipedia , lookup
Object-oriented programming wikipedia , lookup
Go (programming language) wikipedia , lookup
Name mangling wikipedia , lookup
Java (programming language) wikipedia , lookup
C Sharp syntax wikipedia , lookup
Transcript
CS/IS 112 – Week 2 • Logic Problem • More Java background and basics • Values Variables, and operations 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. 2. 3. 4. Claude learned Spanish (which he did not pick up by listening to audio tapes) during an odd-numbered year. He learned Finnish in 2003 He learned Korean by watching vides. 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 __________ __________ __________ __________ _________________________ _________________________ _________________________ _________________________ S 2002 2003 2004 2005 Books Conv Tapes Videos F K P Bk Con Tp Vi 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 The Traditional Translation Process Figure 1.8: Figure 1.9: Compiling and Executing a Java Program A Well-Designed Program Is Built Using Modules 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 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 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 ‘;’ 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 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 } showMessageDialog() Dialog Box: QUESTION_MESSAGE Figure 1.16b: 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 } Data Types •Primitive Data •Operations on primitive data type are provided by arithmetic symbols •Reference Data •Operations provided as methods Figure 2.1: Primitive Data Types Integer data types Type byte short int(def) Storage 1 byte 2 bytes 4 bytes long 8 bytes Range -128 to 127 -32,768 to 32,767 -2,147,483,648 to 2,147,483,647 -9,223,372,036,854,775,808 to 9,223,372,036,865,775,807 Floating Point Type float Storage 4 bytes double(def) 8 bytes Range of values Precision of values Exponential notation Range of Values 1.40129846432481707e-45 to 3.40282346638528860e+38 4.94065645841246544e-324 to 1.79769313486231570e+308 Figure 2.2: Reference Types 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 The Letters JEANS Stored by a Java Program Figure 2.3: Escape Sequences • The backslash (\) character causes Java to interpret the character that follows differently – – – – – – – – \b \f \n \r \t \\ \’ \” move back one space move to the next page move to the next line carriage return move to the next tab setting backslash character output single quote output double quote output 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 An Example of a Value Stored in a Reference Variable Figure 2.8: Figure 2.9a: Creating a Reference Variable Instantiating an Object Figure 2.9b: The Location of Different Strings Using the Same Reference Variable Figure 2.10: 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 { 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); 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); Next Week • Finish Chapter 2 • Start Chapter 3 • All Extra Credit due by 3/13 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)); } }