1 / 8

Recap: “From scratch” projects

Explore the concepts behind software engineering from scratch projects and understand the importance of reusing existing code. Learn about static methods, console projects, and how to use System.out.println to print messages to the console. Discover how to read input from the console and the benefits of using "helper" methods in your code.

becker
Download Presentation

Recap: “From scratch” projects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recap: “From scratch” projects • Today’s software engineering is almost NEVER “from scratch” • Compilers translate from high-level language to machine code • We use libraries for printing and (in Java) much more • We reuse existing code • During a 20 year career, a typical software engineer might: • Work on about 20 projects • Be involved with the creation of only 1 or 2 projects from scratch • For all the other projects, she modifies/extends existing projects • You have done a “from scratch” project called HelloWorld • So that you can see what one is like • And so that you can see “under the hood” of some of the concepts that you have been studying

  2. Hello World concepts – outline • The next slide shows the entire HelloWorld program/It illustrates the following concepts: • The main method • Static methods • Console projects • How to use System.out.println to print a String to the console • A subsequent slide will illustrate these additional concepts: • How to read from the console (i.e., get input from the human) • How and why to use “helper” methods

  3. Hello World – the complete program What is special about the main method? The main method is static. What does that mean? public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world"); } } Is System a class, method or field? How can you tell? Questions? Is out a method or field? How can you tell? Is println a method or field? What does println do?

  4. Answers to questions on previous slide Astatic method is a method that “belongs” to the class instead of to each instance of the class. The static method cannot refer to the class’ non-static fields. The special main method is, by definition, static. main is the name of the special method at which any Java application begins main has command-line arguments sent as a String array public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world"); } } Instructor: run this zipped HelloWorld project System has a public static field called out that is a PrintStream – a thing that can print to the console println is a PrintStream method that prints its argument on a line System is a class that has “system” stuff

  5. HelloWorld extended concepts – outline • We just saw the following concepts from HelloWorld: • The main method • Static methods • Console projects • How to use System.out.println to print a String to the console • Now we turn to these additional concepts from HelloWorld: • How to read from the console (i.e., get input from the human) • How and why to use “helper” methods • We continue to omit comments in order to focus on the code

  6. To read from the console: When you use a library class (like Scanner), you usually need to import the class to tell Java where to find it. import java.util.Scanner; public class ScannerExample { public static void main(String[] args) { Scanner scanner; String input; scanner = new Scanner(System.in); System.out.print(“Enter a String: ”); input = scanner.next(); System.out.println(input.substring(3)); } } Declare a Scanner local variable Construct the Scanner, sending it System.in Prompt the human to type something next() waits for the user to type and returns the result. We could have done anything we want with the input – printing a substring of it is just an example.

  7. “Helper” methods • Encapsulation: bundling things together to make them easier to deal with • Encapsulation in methods: • Bundle a chunk of code • Give it a name • Perhaps give it parameters • “Helper” methods • Private methods that you create to help the rest of your program • Valuable for the same reasons that encapsulating in methods is valuable in general Allows a chunk of code to be referenced via a well-chosen name Allows the code to be reused in a more general way (by sending different values as arguments)

  8. import java.util.Scanner; public class ScannerExample { public static void main(String[] args) { ScannerExample.cubeRoot(12.0); for (int k = 100; k < 110; k = k + 1) { ScannerExample.cubeRoot((double) k); } ScannerExample.cubeRoot(); ScannerExample.cubeRoot(); } private static void cubeRoot(double number) { System.out.println(“Cube root of ” + number + “ is ” + Math.pow(number, 1.0 / 3.0)); } private static void cubeRoot() { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: ”); ScannerExample.cubeRoot(scanner.nextDouble()); } } The boxes are there to help your eyes – not part of the code This slide illustrates: “helper” methods “for” loops Reading numbers from the console Methods that call other methods Study this code. ASK QUESTIONS! You will apply these ideas in HelloWorld, Part 2 (homework). If you wish, download and unzip ScannerExample

More Related