CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CMT Programming Software Applications
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Chapter 3b Standard Input and Output Sample Development.
Objectives You should be able to describe: Data Types
Fundamental concepts in Java. Lesson plan Variable declaration, assign statement & practice Design document & practice.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Input & Output: Console
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
CPS120: Introduction to Computer Science
Chapter 2 Elementary Programming
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Chapter 2: Java Fundamentals
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Chapter 2 Variables.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
Crash course in the Java Programming Language
Elementary Programming
Lecture 2 Data Types Richard Gesick.
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Variables and Primative Types
Interactive Standard Input/output
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2.
IDENTIFIERS CSC 111.
2.1 Parts of a C++ Program.
An overview of Java, Data types and variables
Chapter 2: Basic Elements of Java
Chapter 1: Computer Systems
MSIS 655 Advanced Business Applications Programming
Chapter 3 Numerical Data
Chapter 2 Variables.
Chapter 2: Java Fundamentals
elementary programming
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Variables.
Presentation 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. Claude learned Spanish (which he did not pick up by listening to audio tapes) during an odd-numbered year. 2. He learned Finnish in 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___________________________________

S F K P Bk Con Tp Vi Books Conv Tapes Videos

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

Figure 1.8: The Traditional Translation Process

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 }

Figure 1.16b: showMessageDialog() Dialog Box: QUESTION_MESSAGE

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 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

Floating Point TypeStorageRange of Values float4 bytes e-45 to e+38 double(def)8 bytes e-324 to e+308 Range of values Precision of values Exponential notation

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

Figure 2.3: The Letters JEANS Stored by a Java Program

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

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

Figure 2.8: An Example of a Value Stored in a Reference Variable

Figure 2.9a: Creating a Reference Variable

Figure 2.9b: Instantiating an Object

Figure 2.10: The Location of Different Strings Using the Same Reference Variable

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)); }