DPCompSci.Java.1718.notebook April 29, 2018
|
|
- Paulina Dalton
- 6 years ago
- Views:
Transcription
1 Option D OOP 1
2 if Test a condition to decide whether to execute a block of code. Java Tutorials int x = 10; if (x < 20 { System.out.println(x + " < 20"); Prints 10 < 20 if else Test a condition to decide which of two blocks of code to execute. int x = 10; if (x < 20 { System.out.println(x + " < 20"); else { System.out.println(x + " is not < 20"); Prints Yes, 10 < 20 Can be extended with else if to test multiple conditions. if( x == 10 ) { System.out.print("Value of X is 10"); else if( x == 20 ) { System.out.print("Value of X is 20"); else if( x == 30 ) { System.out.print("Value of X is 30"); else { System.out.print("Something else"); else if's must come before the final else (if there is one) Once an else if succeeds, no further tests are performed. See also: switch Prints the value of x if it's 10, 20, or 30; otherwise prints "Something else". switch Execute a block of code that depends on the value of a variable. Variable must be an integer, string, or enum Can execute multiple cases Use break to exit the switch Default gets executed if no case is matched char grade = 'C'; switch(grade) { case 'A' : System.out.println("Excellent!"); break; case 'B' : case 'C' : System.out.println("Well done"); break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); System.out.println("Your grade is " + grade); Conditionals 2
3 For loop Java Tutorials When you know how many times you need to execute the code block Increment happens after the code block Code block needs to cause the condition to be met if you ever want to exit for (int x = 10; x < 20; x++ ) { Prints value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19 While loop When you don't know how many times you need to execute the code block May never execute Code block needs to cause the condition to be met if you ever want to exit int x = 10; while( x < 20 ) { x++; Prints value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19 Do While loop When you don't know how many times you need to execute the code block Executes the code, then tests the condition. Code block needs to cause the condition to be met if you ever want to exit int x = 10; do { x++; while( x < 20 ) Prints value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19 break Exits the innermost for, while, do while, or switch statement Execution continues after the innermost code block for (int x = 10; x < 20; x++ ) { if (x > 12) break; int x = 10; while( x < 20 ) { if (x > 12) break; x++; All these print value of x : 10 value of x : 11 value of x : 12 int x = 10; do { if (x > 12) break; x++; while( x < 20 ) continue Skips the current iteration of the innermost for, while, do while, or switch statement Execution continues at the start of the code block for (int x = 10; x < 20; x++ ) { if (x > 12 && x < 16) continue; int x = 10; while( x < 20 ) { if (x > 12 && x < 16){ x++; continue; x++; All these print value of x : 10 value of x : 11 value of x : 12 value of x : 16 value of x : 17 value of x : 19 value of x : 20 int x = 10; do { if (x > 12 && x < 16){ x++; continue; x++; while( x < 20 ) Loops 3
4 Keyboard Input using Scanner Java Tutorials The Scanner class handles keyboard input The class is in java.util so you need to import java.util.scanner to use it Start by creating an instance of a Scanner object. Scanner objects have lots of methods to read and interpret input from the keyboard. You can connect the Scanner class to various input sources the console, files, strings, etc. import java.util.scanner; import java.io.*; //optional if you want to declare System.in... variable keyboard points to an object of type Scanner void echome() { Scanner keyboard = new Scanner(new InputStreamReader(System.in)); System.out.println("Enter some text: "); String instr = keyboard.nextline(); System.out.println("You answered:" + instr + ":"); Some of the more useful methods of the Scanner class are: System.in identifies the "standard system input" as the source to be scanned. This is the console. Scanner objects have a nextline() method that waits for the user to type something then returns the line entered. boolean hasnext(string pattern) This method returns true if the next token matches the pattern constructed from the specified string. boolean hasnextint() and boolean hasnext<type>() This method returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextint() method. String next(string pattern) This method returns the next token if it matches the pattern constructed from the specified string. int nextint() and <type> next<type>() This method scans the next token of the input as an int. String nextline() This method advances this scanner past the current line and returns the input that was skipped. Keyboard Input using BufferedReader The BufferedReader class is another way to handle keyboard input The class inherits from java.io.reader and java.io.inputstreamreader so you need to import them both Use this class when you do not need to parse the input but want to read complete lines. See more at Oracle's BufferedReader page Keyboard Input 4
5 File Input using Scanner Java Tutorials The Scanner class also handles file input Connect your Scanner object to a file when you instantiate it. import java.util.scanner; import java.io.*;... //optional if you want to declare System.in void ReadFile() { try { System.out.print("Enter the file name with extension : "); Scanner input = new Scanner(new InputStreamReader(System.in)); File infile = new File(input.nextLine()); input = new Scanner(inFile); while (input.hasnextline()) { String line = input.nextline(); System.out.println(line); input.close(); catch (Exception ex) { ex.printstacktrace(); Keyboard Input using BufferedReader The BufferedReader class is another way to handle keyboard input The class inherits from java.io.reader and java.io.inputstreamreader so you need to import them both Use this class when you do not need to parse the input but want to read complete lines. See more at Oracle's BufferedReader page File Input 5
6 Java Documentation File Operations FileChooser Graphics Create a Frame GUIs in NetBeans Arrays Linked Lists Overview of Q4 Concepts OOP ideas Algorithms Start on IA Main Assignments Mini Project IA Client & Solution IA Conceptual Prototype Some resources: Kjell online book CodingBat Programming by Doing 3/26/18: Q4 Start 6
7 3/26/18 Where are we? You should have a TestFileScanner class in your Exercises project similar to the one below. How did I get this documentation? Let's look. From now on, you will be expected to document your code, with a minimum of a header for each class and method. You may do this after the code is debugged and working, but anything that you turn in must be documented. Today: Start a new BlueJ project called GUITools. In it, create a class called TestGUIFileIO Our first method will be choosefile which uses Java's JFileChooser class. public File choosefile(string title, String defname){ JFileChooser fc = new JFileChooser(); fc.setdialogtitle(title); fc.setcurrentdirectory(new File(defName)); fc.setselectedfile(new File(defName)); int result = fc.showopendialog(null); if (result == JFileChooser.APPROVE_OPTION) { System.out.println("Chose " + fc.getselectedfile()); return fc.getselectedfile(); System.out.println("User cancelled"); //debug return null; //debug JFileChooser is very powerful. Look at some of its other features to, for example, show only directories, manage the look and feel, filter to only certain file types, etc. HW: Read Kjell, Ch 23 Review & Exercise 5 or 6 3/26 File IO 7
8 3/26/18 Here's the code we used on Monday to select a file: public File choosefile(string title, String defname){ JFileChooser fc = new JFileChooser(); fc.setdialogtitle(title); fc.setcurrentdirectory(new File(defName)); fc.setselectedfile(new File(defName)); int result = fc.showopendialog(null); if (result == JFileChooser.APPROVE_OPTION) { System.out.println("Chose " + fc.getselectedfile()); return fc.getselectedfile(); System.out.println("User cancelled"); //debug return null; //debug We saw that, on a Mac, the showopendialog method was not allowing us to type in a file name. The same code works differently in Windows! Option to type in filename To avoid this inconsistency, I found that we can use showdialog instead. It is more customizable than showopendialog and works the same on OSX or Windows. Whew! public File choosefile(string title, String buttontext, String defname){ JFileChooser fc = new JFileChooser(); fc.setdialogtitle(title); fc.setcurrentdirectory(new File(defName)); fc.setselectedfile(new File(defName)); int result = fc.showdialog(null, buttontext); //not showopendialog if (result == JFileChooser.APPROVE_OPTION) { System.out.println("Chose " + fc.getselectedfile()); //debug return fc.getselectedfile(); System.out.println("User cancelled"); //debug return null; JFileChooser is very powerful. Look at some of its other features to, for example, show only directories, manage the look and feel, filter to only certain file types, etc. 3/28/18 Monday 3/26 HW: Read Kjell, Ch 23 Review & Exercise 5 or 6 echofile(): Use choosefile to create a method that opens a text file and echoes the contents to the console. Call it echofile(). Hint: See your readfilewerror method. keyboardtofile(): Use choosefile to open an output file and enter lines from the keyboard that get appended to that file. Call it keyboardtofile(). Be careful not to overwrite a file that you want. And be sure to close the file! appendtofile(): Use choosefile to open an input file and an output file. Append the lines from the input file to the end of the output file. Call it appendtofile(). Be careful not to overwrite a file that you want. And be sure to close both files! loopthroughfile(): Use choosefile to open an input file and an output file. Loop through the lines from the input file and write them to the output file, overwriting it if it exists! Call it LoopThroughFile(). Be careful not to overwrite a file that you want. And be sure to close both files! Notice that loopthroughfile() this could be done directly with Java's Files.copy() method. Why write our own loop? We now have a template for looping through the lines of a file, processing them in some way, and then writing the processed line to the output file. Very powerful! Wednesday 3/28 HW: Read Kjell, Ch 24 3/28 File IO Cont 8
9 (later) DPCompSci.Java.1718.notebook 4/2/18 As we get into more complicated territory, Let's take a time out to go back and review some of the basic constructs that we've seen: Program Organization Projects Packages Classes Objects Methods Functions Variables > "primitive" types > Advanced?, Composite?, Civilized? types Program creation (building) Source Code Compiled Code "Build" or "Make" instruction files. Run Projects File Content Organization imports main() (... later...) Classe(s) Class methods local functions Program flow control (statements) Syntax If then If then else while loops do while loops for loops try catch Packages...in Java...in BlueJ...in NetBeans Classes a "Source Code" file (text;.java) a "Compiled" file (binary;.class) > Made by "compiling" the "source code" into "machine code" (1's & 0's) a "Class Context" file (text;.ctxt) > A "Build instruction file" Method (defining) Methods (calling) <obj>.<method>...later in the same file... Returns a Value of type "File" Methods vs. Functions Basically the same Methods are implicitly called by an object, and Method (defining) so, have access to information about the parent object (this). Java doesn't really have "Calling" Parameters "functions" since all processes are defined in a Class and hence, called methods. Return Value Note: Type must be File...later. still in the same file... To "run" appendfile(), for example Compile TestGUIFileIO (BlueJ creates.class file) > Compilers "import" supporting classes Instantiate a new TestGUIFileIO object > Can be done in the code with a "constructor" method: new <Class> Execute a method from the Object > If needed, BlueJ will prompt you for parameters From another point of view: Variables, run time, & flow control Variables of type File Try Catch construction while loop...later in the same file... "Instantiate" a new object from the class JFileChooser using new "fc is a JfileChooser object" conditional block if...later. still in the same file... primitive type int for loop At "Run time" Events that occur during the time that the program is running. Objects...only exist at run time....are specific "instances" of a Class..."carry with them" the fields and methods of the parent Class...are generally created with a "constructor" method of the Class using new <Class>. Let's look at your Kjell Exercise 23.5 or 23.6 Which of these do you have finished? echofile(): Use choosefile to create a method that opens a text file and echoes the contents to the console. Call it echofile(). Hint: See your readfilewerror method. PrintWriter keyboardtofile(): Use choosefile to open an output file and enter lines from the keyboard that get appended to that file. Call it keyboardtofile(). Be careful not to overwrite a file that you want. And be sure to close the file! Pseudo code for keyboardtofile Choose a file (the output file) Create an input Scanner object from the keyboard Write some general instructions to the user on the console Try to Create a Printwriter connected to the output file Print a prompt, such as ">", to the console Get the next line of input from the console into a string As long as the string is not of zero length Print the next line Print another prompt (">") Get the next line from the console Close the PrintWriter when finished (.close() method) Catch any IO exceptions with a stack trace Close the console 4/2 Questions 9
10 4/4/18 HW Quiz: 1. Generally speaking, what does the code at the right do? 2. Fill in the values that belong in line: 18: 0 [1] 24: sizea [1] 27: suma = suma + value [1] 28: 1. count++ [1] 32: 2. suma/sizea [1] 3. In the code shown, how does the user enter the input file name? In what line? [2] 4. Name at least 2 problems with this technique. [2] 5. Name a better technique. [1] 6. What line(s) of code could you change to make it easier for the user to enter a valid filename? [2] 7. Describe how you would do that. Be as specific as you can. [4] appendtofile(): Use choosefile to open an input file and an output file. Append the lines from the input file to the end of the output file. Call it appendtofile(). Be careful not to overwrite a file that you want. And be sure to close both files! loopthroughfile(): Use choosefile to open an input file and an output file. Loop through the lines from the input file and write them to the output file, overwriting it if it exists! Call it LoopThroughFile(). Be careful not to overwrite a file that you want. And be sure to close both files! Notice that loopthroughfile() this could be done directly with Java's Files.copy() method. Why write our own loop? We now have a template for looping through the lines of a file, processing them in some way, and then writing the processed line to the output file. Very powerful! HW: Write yesno() 4/4 More FileIO 10
11 Download, install NetBeans Creating a graphical user interface with NetBeans 4/9/18 Kjell: Read Ch 46 HW: PBD 136: A little puzzle Create a NetBeans Projects folder somewhere on your computer where you can find it (like in.. \Desert\1718\IBCompSci\NetBeans Projects) Start a new project called NBExercises in your NetBeans Projects folder. Identify the Main class as nbexercises.nbexercises. Create a main method The main() method All java programs that run in a stand alone environment need a main method. The main method can be in any class that is part of the program. The Java virtual machine looks for the main method when a program is first run to "get started". The main method can be in any class that is part of the program. A typical main method might look like: public static void main(string[] args){ try { // Set System L&F UIManager.setLookAndFeel( This instantiates UIManager.getSystemLookAndFeelClassName()); the class <parentclass> program = new <parentclass()>; //the code to run (test?) goes below program.method1totest(); //program.method2totest(); catch (Exception e) { e.printstacktrace(); System.exit(0); // necessary Task: Get your loopthroughfile method running in NetBeans Include choosefile (with 3 parameters and using a parent dialog) Include your working yesno() method How did I get the default name of the file to copy to? Next, work on PBD 136 and finish it for Wed. PBD 136: Open a file specified by the user. This file will contain a bunch of characters. You should read in each character from the file, one character at a time. Display every third character on the screen. Throw the other characters away. There is a sample input file called puzzle.txt, containing a little message you can use to test your program. For fun, the "thrown away" characters might say something, too, in case you care to try to view them somehow. Pseudo code for PBD136 Choose the input file (puzzle.txt or puzzle2.txt) Try to Connect the input file to a Scanner object (so we can read it) As long as there is another line in the input file Get the next line Connect the line to a Scanner so we can loop through characters Set the delimiter to "" so we can get one character at a time As long as the line has another character if the position is a multiple of 3 print the character to the console Close the input file when finished (.close() method) Catch any IO exceptions with a stack trace Hint: On PBD136 you need to do a little trick to use Scanner to read one character at a time. Kjell: Read Ch 46 HW: PBD 136: A little puzzle All further programming work should be done in NetBeans 4/9 Netbeans 11
12 4/11/18 Arrays Java Tutorials HW: PBD Basic Arrays An ordered group of elements Arrays are objects from an Arrays class, with associated methods Elements can be primitive types or objects Need to be declared and created (can be done in one statement) Need to know the size when it's created int[] myintarray; myintarray = new int[10]; //declare an array of integers //create it with 10 elements char[] mychararray = new char[10]; //a 10 element array of chars //you can initialize to values String[] mystringarray = {"Pine", "Fred", "Avacado", "granite"; for (int s: ) { Do the series of exercises PBD , Basic Arrays. Please follow the instructions exactly. me the.java file that contains your code. The method names should be PBD138, PBD139, PBD140, and PBD141 PBD138: Create an array that can hold ten integers. Fill up each slot of the array with the number 113. Then display the contents of the array on the screen, each value on a separate row. Do not use a loop. Also, do not use any variable for the index; you must use literal numbers to refer to each slot. PBD139: Create an array that can hold ten integers. Fill up each slot of the array with the number 113. Then display the contents of the array on the screen, each value on a separate row. This time, you must use a loop, to put the values in the array and also to display them. Also, in the condition of your loop, you should not count up to a literal number. Instead you should use the length field of your array. PBD140: Create an array that can hold ten integers. Fill up each slot of the array with a random number from 1 to 100. Then display the contents of the array on the screen. You must use a loop to fill the array. And, like last time, you must use the length field of your array and not a literal number (like 10) in the condition of the loop. PBD141: Create an array that can hold 1000 integers. Fill the array with random numbers in the range Then display the contents of the array on the screen in rows of 20. You must use a loop to fill the array. If you're careful to only pick random numbers from 10 to 99 and you put two spaces after each number, then your output will line up as below. And if you don't know how to pick random numbers in a certain range, see the nextint() method of the Random class in Javadocs. HW: PBD Basic Arrays 4/11 Arrays 12
13 4/13/18 In class today, you will work on Kjell: Ch 47 Java Tutorials Once you have read it, work on Exercises 4 6 Finish for Monday HW: Kjell Ch 47 Ex 4 6 4/16/18 4/20/18 In class today, and Wed we will work on Kjell: Ch 49C (2D Arrays) Java Tutorials Once you have read it, work on Exercises 1,2,4,5,6 & 7 (optional) Finish for Monday 4/23 HW: Kjell Ch 49C Exercises 1,2,4,5,6 & 7 (optional) 4/23/18 4/27/18 More with arrays and sorting 4/13 Array algorithms 13
14 4/27/18 Sorting Algorithms Exchange sort For each element in the array, starting with the first one: Compare each of the remaining elements to the first one If any element is larger than the first one, swap the two. 3 < 7? i j < 3? i j 20 < 3? i j < 3? i j a[0] = min 12 < 7? i j 20 < 7? i j < 7? i j a[1] = 2nd smallest < 12? i j 16 < 12? i j 8 < 12? i j < 7? i j a[2] = 3rd smallest Bubble sort Selection sort 4/27 Array algorithms 14
15 4/30/18 PBD160: Bubble sort PBD161: Selection sort 5/2/18 Kjell Ch 36: Graphics HW: Kjell Ch 36 Exercises 1 6 5/4/18 Kjell Ch 37: More graphics ideas HW: Kjell Ch 37 Mini Project (Due Wed, 5/9) 5/2 Graphics Intro 15
16 Attachments JavaCheatSheet.pdf DPCS.MiniProject.pdf
Following is the general form of a typical decision making structure found in most of the programming languages:
Decision Making Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined
More informationText User Interfaces. Keyboard IO plus
Text User Interfaces Keyboard IO plus User Interface and Model Model: objects that solve problem at hand. User interface: interacts with user getting input from user giving output to user reporting on
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationJava Input/Output. 11 April 2013 OSU CSE 1
Java Input/Output 11 April 2013 OSU CSE 1 Overview The Java I/O (Input/Output) package java.io contains a group of interfaces and classes similar to the OSU CSE components SimpleReader and SimpleWriter
More informationReading Input from Text File
Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Lab 5 Reading Input from Text File Eng. Mohammed Alokshiya November 2, 2014 The simplest
More informationMidterms Save the Dates!
University of British Columbia CPSC 111, Intro to Computation Alan J. Hu (Using the Scanner and String Classes) Anatomy of a Java Program Readings This Week s Reading: Ch 3.1-3.8 (Major conceptual jump
More informationWentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output
WIT COMP1000 File Input and Output I/O I/O stands for Input/Output So far, we've used a Scanner object based on System.in for all input (from the user's keyboard) and System.out for all output (to the
More informationCSCI 1103: File I/O, Scanner, PrintWriter
CSCI 1103: File I/O, Scanner, PrintWriter Chris Kauffman Last Updated: Wed Nov 29 13:22:24 CST 2017 1 Logistics Reading from Eck Ch 2.1 on Input, File I/O Ch 11.1-2 on File I/O Goals Scanner for input
More informationWhen we reach the line "z = x / y" the program crashes with the message:
CSCE A201 Introduction to Exceptions and File I/O An exception is an abnormal condition that occurs during the execution of a program. For example, divisions by zero, accessing an invalid array index,
More informationBuilding Java Programs
Building Java Programs Chapter 6: File Processing Lecture 6-1: File input using Scanner reading: 6.1-6.2, 5.3 self-check: Ch. 6 #1-6 exercises: Ch. 6 #5-7 Input/output ("I/O") import java.io.*; Create
More informationLesson 3: Accepting User Input and Using Different Methods for Output
Lesson 3: Accepting User Input and Using Different Methods for Output Introduction So far, you have had an overview of the basics in Java. This document will discuss how to put some power in your program
More informationJava provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:
JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators
More informationPROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS Q1. Name any two Object Oriented Programming languages? Q2. Why is java called a platform independent language? Q3. Elaborate the java Compilation process. Q4. Why do we write
More informationCheck out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.
Chapter 4 Lab Loops and Files Lab Objectives Be able to convert an algorithm using control structures into Java Be able to write a while loop Be able to write an do-while loop Be able to write a for loop
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationComputational Expression
Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name
More informationLecture 8 " INPUT " Instructor: Craig Duckett
Lecture 8 " INPUT " Instructor: Craig Duckett Assignments Assignment 2 Due TONIGHT Lecture 8 Assignment 1 Revision due Lecture 10 Assignment 2 Revision Due Lecture 12 We'll Have a closer look at Assignment
More informationThe Java language has a wide variety of modifiers, including the following:
PART 5 5. Modifier Types The Java language has a wide variety of modifiers, including the following: Java Access Modifiers Non Access Modifiers 5.1 Access Control Modifiers Java provides a number of access
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationH212 Introduction to Software Systems Honors
Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing
More informationA token is a sequence of characters not including any whitespace.
Scanner A Scanner object reads from an input source (keyboard, file, String, etc) next() returns the next token as a String nextint() returns the next token as an int nextdouble() returns the next token
More informationJAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop
Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly
More informationCOP 3330 Final Exam Review
COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time
More informationProgram 12 - Spring 2018
CIST 1400, Introduction to Computer Programming Programming Assignment Program 12 - Spring 2018 Overview of Program Earlier this semester, you have written programs that read and processed dates in various
More informationEXCEPTIONS. Fundamentals of Computer Science I
EXCEPTIONS Exception in thread "main" java.lang.numberformatexception: For input string: "3.5" at java.lang.numberformatexception.forinputstring(numberformatexception.java:48) at java.lang.integer.parseint(integer.java:458)
More informationExcep&ons and file I/O
Excep&ons and file I/O Exception in thread "main" java.lang.numberformatexception: For input string: "3.5" at java.lang.numberformatexception.forinputstring(numberformatexception.java:48) at java.lang.integer.parseint(integer.java:458)
More informationPace University. Fundamental Concepts of CS121 1
Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction
More informationBlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages
Topic 1: Basic Java Plan: this topic through next Friday (5 lectures) Required reading on web site I will not spell out all the details in lecture! BlueJ Demo BlueJ = Java IDE (Interactive Development
More informationFile I/O Array Basics For-each loop
File I/O Array Basics For-each loop 178 Recap Use the Java API to look-up classes/method details: Math, Character, String, StringBuffer, Random, etc. The Random class gives us several ways to generate
More informationCSCI 1103: File I/O, Scanner, PrintWriter
CSCI 1103: File I/O, Scanner, PrintWriter Chris Kauffman Last Updated: Mon Dec 4 10:03:11 CST 2017 1 Logistics Reading from Eck Ch 2.1 on Input, File I/O Ch 11.1-2 on File I/O Goals Scanner for input Input
More informationCS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8
CS 302: INTRODUCTION TO PROGRAMMING Lectures 7&8 Hopefully the Programming Assignment #1 released by tomorrow REVIEW The switch statement is an alternative way of writing what? How do you end a case in
More informationSt. Edmund Preparatory High School Brooklyn, NY
AP Computer Science Mr. A. Pinnavaia Summer Assignment St. Edmund Preparatory High School Name: I know it has been about 7 months since you last thought about programming. It s ok. I wouldn t want to think
More informationFile class in Java. Scanner reminder. File methods 10/28/14. File Input and Output (Savitch, Chapter 10)
File class in Java File Input and Output (Savitch, Chapter 10) TOPICS File Input Exception Handling File Output Programmers refer to input/output as "I/O". Input is received from the keyboard, mouse, files.
More informationJava How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.
Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;
More informationFull file at
Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class
More informationSorting/Searching and File I/O
Sorting/Searching and File I/O Sorting Searching CLI: File Input CLI: File Output GUI: File Chooser Reading for this lecture: 13.1 (and other sections of Chapter 13) 1 Sorting Sorting is the process of
More informationCHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS
CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS Computers process information and usually they need to process masses of information. In previous chapters we have studied programs that contain a few variables
More informationFile Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.
File Input/Output Tuesday, July 25 2006 CSE 1020, Summer 2006, Overview (1): Before We Begin Some administrative details Some questions to consider The Class Object What is the Object class? File Input
More informationSimple Java Input/Output
Simple Java Input/Output Prologue They say you can hold seven plus or minus two pieces of information in your mind. I can t remember how to open files in Java. I ve written chapters on it. I ve done it
More informationCOMP-202 More Complex OOP
COMP-202 More Complex OOP Defining your own types: Remember that we can define our own types/classes. These classes are objects and have attributes and behaviors You create an object or an instance of
More informationJava Foundations Certified Junior Associate
Java Foundations Certified Junior Associate 习题 1. When the program runs normally (when not in debug mode), which statement is true about breakpoints? Breakpoints will stop program execution at the last
More informationCOMP102: 181. Menu. More while loops. Admin: Test. Peter Andreae
Menu COMP102: 181 More while loops Admin: Test Designing loops with numbers COMP102: 182 When the number of steps is known at the beginning of the loop: int count = 0; int num = 1; while ( count < number)
More informationAnnouncements. 1. Forms to return today after class:
Announcements Handouts (3) to pick up 1. Forms to return today after class: Pretest (take during class later) Laptop information form (fill out during class later) Academic honesty form (must sign) 2.
More information09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.
CSE 143 Java Streams Reading: 19.1, Appendix A.2 GREAT IDEAS IN COMPUTER SCIENCE REPRESENTATION VS. RENDERING 4/28/2002 (c) University of Washington 09-1 4/28/2002 (c) University of Washington 09-2 Topics
More informationA+ Computer Science -
import java.util.scanner; or just import java.util.*; reference variable Scanner keyboard = new Scanner(System.in); object instantiation Scanner frequently used methods Name nextint() nextdouble() nextfloat()
More informationCourse Outline. Introduction to java
Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.
More informationFirst Java Program - Output to the Screen
First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the
More informationCSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O
CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O 1 Sending Output to a (Text) File import java.util.scanner; import java.io.*; public class TextFileOutputDemo1 public static void
More informationExceptions. When do we need exception mechanism? When and why use exceptions? The purpose of exceptions
Exceptions When do we need exception mechanism? When and why use exceptions? The purpose of exceptions Exceptions An exception is thrown when there is no reasonable way for the code that discovered the
More informationCS 112 Introduction to Programming
CS 112 Introduction to Programming Summary of Methods; User Input using Scanner Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin
More informationComputational Expression
Computational Expression Scanner, Increment/Decrement, Conversion Janyl Jumadinova 17 September, 2018 Janyl Jumadinova Computational Expression 17 September, 2018 1 / 11 Review: Scanner The Scanner class
More informationOverview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp
Overview CSE 143 Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Ch. 16 4/27/2004 (c) 2001-4, University of
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management
More informationChapter 4: Loops and Files
Chapter 4: Loops and Files Chapter Topics Chapter 4 discusses the following main topics: The Increment and Decrement Operators The while Loop Using the while Loop for Input Validation The do-while Loop
More informationF1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept
F1 A Java program Ch 1 in PPIJ Introduction to the course The computer and its workings The algorithm concept The structure of a Java program Classes and methods Variables Program statements Comments Naming
More informationMidterms Save the Dates!
University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Errors (Using the Scanner and String Classes) Anatomy of a Java Program Readings This Week s Reading: Ch 3.1-3.8 (Major conceptual
More informationCIS 110: Introduction to Computer Programming
CIS 110: Introduction to Computer Programming Lecture 15 Our Scanner eats files ( 6.1-6.2) 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 1 Outline Programming assertion recap The Scanner object
More informationChapter 4: Loops and Files
Chapter 4: Loops and Files Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 4 discusses the following main topics: The Increment and Decrement
More informationFundamentals of Programming Data Types & Methods
Fundamentals of Programming Data Types & Methods By Budditha Hettige Overview Summary (Previous Lesson) Java Data types Default values Variables Input data from keyboard Display results Methods Operators
More informationCS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG
CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignment Class Exercise 19 is assigned Homework 8 is assigned Both Homework 8 and Exercise 19 are
More informationCOMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015
COMP-202: Foundations of Programming Lecture 22: File I/O Jackie Cheung, Winter 2015 Announcements Assignment 5 due Tue Mar 31 at 11:59pm Quiz 6 due Tue Apr 7 at 11:59pm 2 Review 1. What is a graph? How
More informationProgramming with Java
Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules
More informationObject Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang
Object Oriented Programming and Design in Java Session 2 Instructor: Bert Huang Announcements TA: Yipeng Huang, yh2315, Mon 4-6 OH on MICE clarification Next Monday's class canceled for Distinguished Lecture:
More informationBBM 102 Introduction to Programming II Spring Exceptions
BBM 102 Introduction to Programming II Spring 2018 Exceptions 1 Today What is an exception? What is exception handling? Keywords of exception handling try catch finally Throwing exceptions throw Custom
More information! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like
Indefinite loops while loop! indefinite loop: A loop where it is not obvious in advance how many times it will execute.! We often use language like " "Keep looping as long as or while this condition is
More informationCS 170 Java Programming 1. Week 10: Loops and Arrays
CS 170 Java Programming 1 Week 10: Loops and Arrays What s the Plan? Topic 1: A Little Review Use a counted loop to create graphical objects Write programs that use events and animation Topic 2: Advanced
More informationCOMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed
1 1 COMP200 INPUT/OUTPUT OOP using Java, based on slides by Shayan Javed Input/Output (IO) 2 3 I/O So far we have looked at modeling classes 4 I/O So far we have looked at modeling classes Not much in
More informationOverview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au
Overview CSE 143 Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Sec. 19.1, Appendix A2 11/2/2003 (c) 2001-3,
More informationOverview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE
Overview CSE 143 Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Sec. 19.1, Appendix A2 11/2/2003 (c) 2001-3,
More informationCS244 Advanced Programming Applications
CS 244 Advanced Programming Applications Exception & Java I/O Lecture 5 1 Exceptions: Runtime Errors Information includes : class name line number exception class 2 The general form of an exceptionhandling
More informationI/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O
I/O 10-7-2013 I/O in Java I/O streams vs. Reader/Writer HW#3 due today Reading Assignment: Java tutorial on Basic I/O public class Swimmer implements Cloneable { public Date geteventdate() { return (Date)
More informationOutline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.
OOP in Java 1 Outline 1. Getting started, primitive data types and control structures 2. Classes and objects 3. Extending classes 4. Using some standard packages 5. OOP revisited Parts 1 to 3 introduce
More informationAP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS
AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS PAUL L. BAILEY Abstract. This documents amalgamates various descriptions found on the internet, mostly from Oracle or Wikipedia. Very little of this
More informationUsing Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6
Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Creating
More informationLoops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department
Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 6 Loops
More informationLecture 10. Instructor: Craig Duckett
Lecture 10 Instructor: Craig Duckett Announcements Assignment 1 Revision DUE TONIGHT in StudentTracker by midnight If you have not yet submitted an Assignment 1, this is your last chance to do so to earn
More informationIntroduction to Classes and Objects Pearson Education, Inc. All rights reserved.
1 3 Introduction to Classes and Objects 2 You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility.
More informationOverview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi
CSE 143 Overview Topics Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Ch. 16 2/3/2005 (c) 2001-5, University of Washington 12-1 2/3/2005 (c) 2001-5,
More informationCONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice
CONTENTS: Arrays Strings COMP-202 Unit 5: Loops in Practice Computing the mean of several numbers Suppose we want to write a program which asks the user to enter several numbers and then computes the average
More information13 th Windsor Regional Secondary School Computer Programming Competition
SCHOOL OF COMPUTER SCIENCE 13 th Windsor Regional Secondary School Computer Programming Competition Hosted by The School of Computer Science, University of Windsor WORKSHOP I [ Overview of the Java/Eclipse
More informationBuilding Java Programs
Building Java Programs Chapter 6 File Input with Scanner reading: 6.1 6.2, 5.4 2 Input/output (I/O) import java.io.*; Create a File object to get info about a file on your drive. (This doesn't actually
More informationCSPP : Introduction to Object-Oriented Programming
CSPP 511-01: Introduction to Object-Oriented Programming Harri Hakula Ryerson 256, tel. 773-702-8584 hhakula@cs.uchicago.edu August 7, 2000 CSPP 511-01: Lecture 15, August 7, 2000 1 Exceptions Files: Text
More informationProject #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.
Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently. User Request: Create a simple magazine data system. Milestones:
More informationChapter 2. Elementary Programming
Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,
More informationFull file at
Chapter 1 Primitive Java Weiss 4 th Edition Solutions to Exercises (US Version) 1.1 Key Concepts and How To Teach Them This chapter introduces primitive features of Java found in all languages such as
More informationCOMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015 Announcements Assignment 2 posted and due on 30 th of May (23:30). Extra class tomorrow
More informationGetting started with Java
Getting started with Java by Vlad Costel Ungureanu for Learn Stuff Programming Languages A programming language is a formal constructed language designed to communicate instructions to a machine, particularly
More informationUnit 10: exception handling and file I/O
Unit 10: exception handling and file I/O Using File objects Reading from files using Scanner Writing to file using PrintStream not in notes 1 Review What is a stream? What is the difference between a text
More informationProject 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java
Java Data types and input/output Sharma Chakravarthy Information Technology Laboratory (IT Lab) Computer Science and Engineering Department The University of Texas at Arlington, Arlington, TX 76019 Email:
More informationThere are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:
Project MindNumber Collaboration: Solo. Complete this project by yourself with optional help from section leaders. Do not work with anyone else, do not copy any code directly, do not copy code indirectly
More informationLab Exercise 1. Objectives: Part 1. Introduction
Objectives: king Saud University College of Computer &Information Science CSC111 Lab Object II All Sections - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
More informationIntroduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview
Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple
More informationIntroduction to Classes and Objects
1 2 Introduction to Classes and Objects You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility.
More informationOverview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE
CSE 143 Overview Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Ch. 16 10/20/2004 (c) 2001-4, University of
More informationGPolygon GCompound HashMap
Five-Minute Review 1. How do we construct a GPolygon object? 2. How does GCompound support decomposition for graphical objects? 3. What does algorithmic complexity mean? 4. Which operations does a HashMap
More informationCS Programming I: File Input / Output
CS 200 - Programming I: File Input / Output Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624
More informationCONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction
CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics COMP-202 Unit 1: Introduction Announcements Did you miss the first lecture? Come talk to me after class. If you want
More informationFiles & Exception Handling. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington
Files & Exception Handling CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Exception Handling (try catch) Suppose that a line of code may make your
More informationExample Program. public class ComputeArea {
COMMENTS While most people think of computer programs as a tool for telling computers what to do, programs are actually much more than that. Computer programs are written in human readable language for
More informationChapter 4: Control Structures I
Chapter 4: Control Structures I Java Programming: From Problem Analysis to Program Design, Second Edition Chapter Objectives Learn about control structures. Examine relational and logical operators. Explore
More information