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)