Changes between Version 10 and Version 11 of ControlSystems/SoftwareTeam/Training/GettingStarted/IntroJava


Ignore:
Timestamp:
Oct 1, 2019, 11:10:36 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/IntroJava

    v10 v11  
    9797}
    9898}}}
    99 * Save this program as Loop Example
     99* Save this program as !LoopExample.java
    100100* Run the program in the debugger with a breakpoint set at the line while (counter <= 10)
    101101* Step through the program a line at a time, observing the program output, variables, and flow of execution
     102* There are three types of [https://www.geeksforgeeks.org/loops-in-java/ loops in Java] including while, for, and do loops.  Read about them and try a few!
     103
     104== Functions/Methods
     105All modern programming languages allow you to extend the language by creating new command words.  In Java, a new word is called a function.  Functions are like machines that have an input and an output; you put something(s) into the function execute the function and the function does something that might include providing an output.  Every Java function has 4 primary characteristics:
     106* a name
     107* optional inputs (called parameters)
     108* a body (the code that defines what the function does)
     109* an optional output (called a return value)
     110Consider the following example of a function:
     111{{{
     112   int squared(int a) {
     113       return a * a;
     114   }
     115}}}
     116The function's name is 'squared' and it takes an input parameter that is an integer and is referred to within the function as a. The function returns an integer that it computes by multiplying a by itself.
     117
     118Functions can take more than one parameter, but they can only return 1 value (or no values).  Consider this function that takes two input parameters and returns no value:
     119{{{
     120   void sayit(String msg, int times) {
     121        for (int n=0; n<times; n++) {
     122            System.out.println(msg);
     123        }
     124   }
     125}}}
     126
     127Open your Hello World project folder and modify the Hello.java program as follows:
     128{{{
     129public class Hello {
     130
     131    static void sayit(String msg, int times) {
     132        for (int n=0; n<times; n++) {
     133            System.out.println(msg);
     134        }
     135   }
     136    public static void main(String args[]) {
     137        System.out.println("Hello world");
     138        sayit("howdy", 10);
     139    }
     140}
     141}}}
     142
     143* Run the program and observe the output
     144* For now, we're going to ignore the keywords public and static, we'll get to those soon
     145
     146== Objects and Classes
     147
     148...Coming soon...
    102149
    103150== Princeton Java