Computer Science 209 Software Development IO 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 %. 2 f" % area) main()
![Java Version import java. util. Scanner; public class Circle. Area{ public static void main(String[] Java Version import java. util. Scanner; public class Circle. Area{ public static void main(String[]](http://slidetodoc.com/presentation_image_h2/65b56df40aa5aa6d719d3d8e87c43279/image-4.jpg)
Java Version import java. util. Scanner; public class Circle. Area{ public static void main(String[] args){ Scanner input = new Scanner(System. in); System. out. print("Enter the radius: "); double radius = input. next. Double(); double area = Math. PI * Math. pow(radius, 2); System. out. println("The area is " + area); System. out. printf("The area is %. 2 fn", area); } }
![Import Statements import java. util. Scanner; public class Circle. Area{ public static void main(String[] Import Statements import java. util. Scanner; public class Circle. Area{ public static void main(String[]](http://slidetodoc.com/presentation_image_h2/65b56df40aa5aa6d719d3d8e87c43279/image-5.jpg)
Import Statements import java. util. Scanner; public class Circle. Area{ 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 Circle. Area{ public static void main(String[] Object Instantiation import java. util. Scanner; public class Circle. Area{ public static void main(String[]](http://slidetodoc.com/presentation_image_h2/65b56df40aa5aa6d719d3d8e87c43279/image-6.jpg)
Object Instantiation import java. util. Scanner; public class Circle. Area{ 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 Circle. Area{ public static void main(String[] Variable Declaration import java. util. Scanner; public class Circle. Area{ public static void main(String[]](http://slidetodoc.com/presentation_image_h2/65b56df40aa5aa6d719d3d8e87c43279/image-7.jpg)
Variable Declaration import java. util. Scanner; public class Circle. Area{ 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 Circle. Area{ public static void main(String[] Numeric Types import java. util. Scanner; public class Circle. Area{ public static void main(String[]](http://slidetodoc.com/presentation_image_h2/65b56df40aa5aa6d719d3d8e87c43279/image-8.jpg)
Numeric Types import java. util. Scanner; public class Circle. Area{ public static void main(String[] args){ Scanner input = new Scanner(System. in); System. out. print("Enter the radius: "); double radius = input. next. Double(); double area = Math. PI * Math. pow(radius, 2); double, float, long, and int are the most commonly used numeric types

Java vs Python double radius = 25. 5; . . . radius += 2; . . radius = "Hi there!"; Syntax error caught at compile time radius = 25. 5. . . radius += 2. . radius = "Hi there!" 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); Syntax error caught at compile time radius = 25. . . radius += 2. 5. . print(radius); No problem! All variables in Java have a type associated with them
![Numeric Input import java. util. Scanner; public class Circle. Area{ public static void main(String[] Numeric Input import java. util. Scanner; public class Circle. Area{ public static void main(String[]](http://slidetodoc.com/presentation_image_h2/65b56df40aa5aa6d719d3d8e87c43279/image-11.jpg)
Numeric Input import java. util. Scanner; public class Circle. Area{ public static void main(String[] args){ Scanner input = new Scanner(System. in); System. out. print("Enter the radius: "); double radius = input. next. Double(); double area = Math. PI * Math. pow(radius, 2); The Scanner method next. Double automatically converts the user’s input to a number

static Variables and Methods import java. util. Scanner; public class Circle. Area{ public static void main(String[] args){ Scanner input = new Scanner(System. in); System. out. print("Enter the radius: "); double radius = input. next. Double(); 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 Circle. Area{ 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 log 2(100), or 7

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

For Wednesday Get started on Programming Project 1 (on Sakai)
- Slides: 18