Week 13 - Friday
What did we talk about last time? Finished inheritance example Files
Reading from a text file is almost ridiculously easy We use Scanner , just like reading from the command line We just have to create a new File object that gives the file we want to read from Scanner in = new Scanner(new File("input.txt")); This code will read from some file called input.txt , as if someone were typing its contents into the command line
The alternative to catching an exception is throwing it up to the next level, making it someone else's problem Sure, your program will crash if no one deals with it, but at least your code will compile We do this by putting a throws FileNotFoundException on the declaration of main() (or whatever method we're in) public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(new File("input.txt"));
Java loves objects If you want to write to a file, you've got to create a PrintWriter object, based on a FileOutputStream object (which takes the file name as a parameter) PrintWriter out = new PrintWriter(new FileOutputStream ("output.txt")); Once you've got a PrintWriter , you can use it just like System.out
Unlike the command line, you should really close files when you're done reading from them If you forget to close a file you’re writing to, everything you write might not show up And, for situations where you're accessing multiple files, it may be even more important to close them Scanner in = new Scanner(new File("input.txt")); PrintWriter out = new PrintWriter(new FileOutputStream ("output.txt")); //do stuff in.close(); out.close();
Let's write a program that prompts the user for 1o int values and then writes them to a file called numbers.txt Then, let's write another program that opens numbers.txt , reads all 10 numbers, sorts them, and prints them out in order
Let's write a program that prints the first million prime numbers to a file
Format: Multiple choice questions (~20%) Short answer questions (~20%) Programming problems (~60%) Online No notes Closed book No calculator
Designed to be 50% longer than previous exams But you'll have 100% more time Time: Wednesday, 12/02/2020, 8:00 - 10:00 a.m. Online If you are entitled to extra time, please contact me as soon as possible to arrange how that extra time will be scheduled
History of computers Hardware Software development Basic Java syntax Output with System.out.print()
Mechanical Calculation Devices (2400BC onward) • Aid to human calculation • No stored program Mechanical Computers (1725 onward) • Punch card programming • Serious limitations Early Electronic Computers (1941 onward) • General purpose, stored program computers • Electronic, using vacuum tubes Microprocessors (1970's onward) • Succeeded transistors • Now billions of computations per second at a nanometer scale
Often goes through phases similar to the following: 1. Understand the problem 2. Plan a solution to the problem 3. Implement the solution in a programming language 4. Test the solution 5. Maintain the solution and do bug fixes Factor of 10 rule!
Source Machine Code Code Hardware 010101010 Computer! Solve a 010100101 Execute problem; 001110010
Java Source Java Machine Hardware Code Code Bytecode class A { 101110101 010101010 Problem p; 101011010 010100101 JVM p.solve(); 110010011 001110010 }
The absolute smallest program possible, with a print statement public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); } }
For example, instead of one print statement, we can have several: System.out.println("Hello, world!"); System.out.println("Hello, galaxy!"); System.out.println("Goodbye, world!"); Each statement is an instruction to the computer They are printed in order, one by one
Java is a case sensitive language Class is not the same as class System.out.println("Word!"); prints correctly system.Out.Println("Word!"); does not compile
Java generally ignores whitespace (tabs, newlines, and spaces) System.out.println("Hello, world!"); is the same as: System.out. println( "Hello, world!"); You should use whitespace effectively to make your code readable
There are two kinds of comments (actually 3) Single line comments use // System.out.println("Hi!"); // this is a comment Multi-line comments start with a /* and end with a */ System.out.println("Hi!"); /* this is a multi-line comment */
Binary representation Basic data types Using Scanner for input Numerical operations
The binary number system is base 2 This means that its digits are: 0 and 1 Base 2 means that you need 2 digits to represent two, namely 1 and 0 Each place in the number as you move left corresponds to an increase by a factor of 2 instead of 10
Sixty fours 256’s Sixteens Fours 11111100100 1024’s Ones 512’s Twos 128’s Eights Thirty twos
We're focusing on five basic types of data in Java These are: int For whole numbers double For rational numbers boolean For true or false values char For single characters String For words String is a little different from the rest, since you can call methods on it (and for other reasons)
Type Sample Literals Kind of values -5 int 0 Integers 900031 3.14 Floating-point double -0.6 Numbers 6.02e23 true boolean Boolean values false 'A' char 'Z' Single characters '&' Sequences of "If you dis Dr. Dre" String "10 Sesquipedalians" characters
The int type is used to store integers (positive and negative whole numbers and zero) Examples: 54 -893992 0 Inside the computer, an int takes up 4 bytes of space, which is 32 bits (1's and 0's)
You will use the int type very often Sometimes, however, you need to represent numbers with a fractional part The double type is well suited to this purpose Declaration of a double variable is just like an int variable: double x;
Numbers are great But, sometimes you only need to keep track of whether or not something is true or false This is what the boolean type is for Hopefully you have more appreciation for boolean s now Declaration of a boolean variable is like so: boolean value;
Sometimes you need to deal with characters This is what the char type is for The char type only allows you to store a single character like '$' or 'q' Declaration of a char variable is like so: char c;
The String type is different from the other types in several ways The important thing for you to focus on now is that it can hold a large number of char s, not just a single value A String literal is what we used in the Hello, World program String word;
There are three parts to using Scanner for input Include the appropriate import statement so that your program 1. knows what a Scanner object is 2. Create a specific Scanner object with a name you choose Use the object you create to read in data 3.
Lots of people have written all kinds of useful Java code By importing that code, we can use it to help solve our problems To import code, you type import and then the name of the package or class To import Scanner , type the following at the top of your program (before the class !) import java.util.Scanner;
Once you have imported the Scanner class, you have to create a Scanner object To do so, declare a reference of type Scanner , and use the new keyword to create a new Scanner with System.in as a parameter like so: Scanner in = new Scanner(System.in); You can call it whatever you want, I chose to call it in
Now that you've got a Scanner object, you can use it to read some data It has a method that will read in the next piece of data that user types in, but you have to know if that data is going to be an int , a double , or a String Let's say the user is going to input her age (an int ) and you want to store it in an int variable called years We'll use the nextInt() method to do so: int years = in.nextInt();
Scanner has a lot of methods (ways to accomplish some tasks) For now, we're only interested in three These allow us to read the next int , the next double , and the next String , respectively: Scanner in = new Scanner(System.in); int number = in.nextInt(); double radius = in.nextDouble(); String word = in.next();
+ adds - subtracts * multiplies / divides (integer division for int type and fractional parts for double type) % finds the remainder
Order of operations holds like in math int a = 31; int b = 16; int c = 1; int d = 2; a = (((b + c) * d) – a / b) / d; You can use parentheses to clarify or change the precedence Now a is 16
Recommend
More recommend