Software Development I/O and Numbers

Slides:



Advertisements
Similar presentations
Computer Programming Lab 8.
Advertisements

CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
Computer Programming Lab(4).
Computer Programming Lab(5).
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Fundamentals Expanded SE1021 Dr. Mark L. Hornick 1.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Functions and Modules CSIS 1595: Fundamentals of Programming and Problem Solving 1.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
1 Chapter 2 Elementary Programming. 2 Objectives  To write Java programs to perform simple calculations  To obtain input from the console using the.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Chapter 2 Elementary Programming
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
EXAM 1 REVIEW. days until the AP Computer Science test.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
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.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Introduction to programming in java
CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1.
CompSci 230 S Programming Techniques
Chapter 2 Elementary Programming
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Unit 2 Elementary Programming
Elementary Programming
Formatted Output (printf)
Chapter 2 Elementary Programming
Exceptions and User Input Validation
Data types, Expressions and assignment, Input from User
TK1114 Computer Programming
First Programs CSE 1310 – Introduction to Computers and Programming
Something about Java Introduction to Problem Solving and Programming 1.
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
CSE 1020:Programming by Delegation
Java so far Week 7.
בתרגול הקודם אתר הקורס (הודעות, פרטי סגל הקורס, עבודות, פורום, מערכת הגשת תרגילים וכו') שימוש בחלון ה-command, פקודות בסיסות קוד Java: הידור (= קומפילציה)
Fundamentals 2.
Chapter 2 Elementary Programming
Chapter 2 Elementary Programming
Variables, Types, Operations on Numbers
Lecture Notes – Week 2 Lecture-2
Introduction to Java Brief history of Java Sample Java Program
CS2011 Introduction to Programming I Elementary Programming
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
First Programs CSE 1310 – Introduction to Computers and Programming
Chapter 2 Elementary Programming
Chapter 2: Beginning to Program
Presentation transcript:

Software Development I/O and Numbers Computer Science 209 Software Development I/O and Numbers

Example: Circle Area Input the radius Compute the area Output the area

Python Version import math def main(): radius = input("Enter the radius: ") area = math.pi * radius ** 2 print("The area is", area) print("The area is %.2f" % area) main()

Java Version import java.util.Scanner; public class CircleArea{ public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter the radius: "); double radius = input.nextDouble(); double area = Math.PI * Math.pow(radius, 2); System.out.println("The area is " + area);: System.out.printf("The area is %.2f\n", area); }

Import Statements Some classes are not built in import java.util.Scanner; public class CircleArea{ public static void main(String[] args){ Scanner input = new Scanner(System.in); Some classes are not built in They must be imported from packages (like modules) Class names are capitalized; package names are lowercase

Object Instantiation import java.util.Scanner; public class CircleArea{ public static void main(String[] args){ Scanner input = new Scanner(System.in); New instances or objects of a class are created with the new operator As in Python, the constructor is the class name, which is called as if it were a method, with any expected arguments

Variable Declaration import java.util.Scanner; public class CircleArea{ public static void main(String[] args){ Scanner input = new Scanner(System.in); When new variable, parameter, or method names are introduced, they must be prefixed with an explicit type name This allows the compiler to do type checking at compile time, so type incompatibility errors will not occur at runtime

Numeric Types import java.util.Scanner; public class CircleArea{ public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter the radius: "); double radius = input.nextDouble(); double area = Math.PI * Math.pow(radius, 2); double, float, long, and int are the most commonly used numeric types

Java vs Python Syntax error caught at compile time No problem! double radius = 25.5; . radius += 2; radius = "Hi there!"; radius = 25.5 . radius += 2 radius = "Hi there!" Syntax error caught at compile time No problem! All variables in Java have a type associated with them

Explicit vs Implicit Casting int radius = 25; . radius += 2.5; System.out.println(radius); radius = 25 . radius += 2.5 print(radius); Syntax error caught at compile time No problem! All variables in Java have a type associated with them

Numeric Input import java.util.Scanner; public class CircleArea{ public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter the radius: "); double radius = input.nextDouble(); double area = Math.PI * Math.pow(radius, 2); The Scanner method nextDouble automatically converts the user’s input to a number

static Variables and Methods import java.util.Scanner; public class CircleArea{ public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter the radius: "); double radius = input.nextDouble(); double area = Math.PI * Math.pow(radius, 2); Math.PI is a static variable and Math.pow is a static method Many math constants and methods are in the Math class

The + Operator import java.util.Scanner; public class CircleArea{ public static void main(String[] args){ . double area = Math.PI * Math.pow(radius, 2); System.out.println("The area is " + area); The + operator works with a string and a number as operands, converting the number to a string before performing the concatenation

Example: Guess the Number The computer thinks of a random number between 1 and 100 The user guesses a number until she hits it or exceeds the maximum number of guesses needed The maximum is log2(100), or 7

Python Version import math import random def main(): myNumber = random.randint(1, 100) count = 0 print("I'm thinking of a number between 1 and 100.") while True: count += 1 userNumber = input("Enter your guess: ") if userNumber == myNumber: print("You guessed it in", count, "attempts!") break elif userNumber < myNumber: print("Too small!") else: print("Too large!") if count == round(math.log(100, 2)): print("Exceeded maximum allowable guesses!”)

Math Class Methods Very much like the math module functions in Python No need to import anything >>> import math >>> print(math.log(256, 2)) 8.0 System.out.println(Math.log(256) / Math.log(2)); // Prints 8.0

Rounding vs Truncating >>> import math >>> x = math.log(100, 2)) >>> x 6.643856189774725 >>> int(x) 6 >>> round(x) 7 double x = Math.log(100) / Math.log(2); System.out.println(x); // 6.643856189774725 System.out.println((int) x); // Prints 6 System.out.println(Math.round(x)); // Prints 7

Get started on Programming Project 1 (on Sakai) For Wednesday Get started on Programming Project 1 (on Sakai)