Skip to content

Commit 1907e72

Browse files
committed
add example for terminal
1 parent 492b245 commit 1907e72

File tree

8 files changed

+249
-2
lines changed

8 files changed

+249
-2
lines changed

.DS_Store

0 Bytes
Binary file not shown.

HelloWorldTerminal.zip

4.13 KB
Binary file not shown.
2.08 KB
Binary file not shown.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import java.util.Scanner;
2+
3+
public class ConsoleProgram{
4+
5+
private Scanner scanner;
6+
7+
public static void main(String[] args){
8+
// Assume the class name is passed in as the first argument.
9+
10+
if(args.length == 0){
11+
System.out.println("Please provide the name of the main class as an argument.");
12+
return;
13+
}
14+
15+
String mainClassName = args[0];
16+
17+
try{
18+
Class mainClass = Class.forName(mainClassName);
19+
Object obj = mainClass.newInstance();
20+
ConsoleProgram program = (ConsoleProgram)obj;
21+
program.run();
22+
} catch (IllegalAccessException ex) {
23+
System.out.println("Error in program. Make sure you extend ConsoleProgram");
24+
} catch (InstantiationException ex) {
25+
System.out.println("Error in program. Make sure you extend ConsoleProgram");
26+
} catch (ClassNotFoundException ex) {
27+
System.out.println("Error in program. Make sure you extend ConsoleProgram");
28+
}
29+
}
30+
31+
/**
32+
* The run method is overwritten by the subclass. This is the main entry point for
33+
* ConsolePrograms. There is no main method in student programs that extend ConsoleProgram,
34+
* instead they write their application starting like this:
35+
*
36+
* public void run()
37+
* {
38+
* // student code here
39+
* }
40+
*
41+
* The main method of ConsoleProgram calls this run method when it begins.
42+
*/
43+
public void run(){
44+
/* Overridden by subclass */
45+
}
46+
47+
/**
48+
* Constructor for Console program. Create a scanner instance to be used for user input.
49+
*/
50+
public ConsoleProgram(){
51+
scanner = new Scanner(System.in);
52+
}
53+
54+
/**
55+
* This method reads a line of input from the user, given a prompt.
56+
*
57+
* @param prompt A prompt to the user to get input
58+
* @return A String of the user input.
59+
*/
60+
public String readLine(String prompt){
61+
System.out.print(prompt);
62+
return scanner.nextLine();
63+
}
64+
65+
/**
66+
* This method reads a boolean value from the user, asking them for either
67+
* a true or false value. This makes use of the readLine method.
68+
*
69+
* @param prompt A prompt to the user to read a boolean value
70+
* @return A boolean value read from the user.
71+
*/
72+
public boolean readBoolean(String prompt){
73+
74+
while(true){
75+
String input = readLine(prompt);
76+
77+
if(input.equalsIgnoreCase("true")){
78+
return true;
79+
}
80+
81+
if(input.equalsIgnoreCase("false")){
82+
return false;
83+
}
84+
}
85+
}
86+
87+
/**
88+
* This method reads a double value from the user, given a prompt.
89+
*
90+
* @param prompt A prompt to the user to ask for a a double.
91+
* @return A double value read from the user.
92+
*/
93+
public double readDouble(String prompt){
94+
95+
while(true){
96+
String input = readLine(prompt);
97+
try {
98+
double n = Double.valueOf(input).doubleValue();
99+
return n;
100+
} catch (NumberFormatException e){
101+
102+
}
103+
}
104+
}
105+
106+
/**
107+
* This method reads a int value from the user, given a prompt.
108+
*
109+
* @param prompt A prompt to the user to ask for an int
110+
* @return The int value read from the user.
111+
*/
112+
public int readInt(String prompt){
113+
114+
while(true){
115+
String input = readLine(prompt);
116+
try {
117+
int n = Integer.parseInt(input);
118+
return n;
119+
} catch (NumberFormatException e){
120+
121+
}
122+
}
123+
}
124+
125+
}

HelloWorldTerminal/Hello.class

388 Bytes
Binary file not shown.

HelloWorldTerminal/Hello.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Hello extends ConsoleProgram
2+
{
3+
public void run()
4+
{
5+
System.out.println("Hello World!");
6+
}
7+
}

HelloWorldTerminal/Randomizer.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.util.Random;
2+
3+
public class Randomizer{
4+
5+
public static Random theInstance = null;
6+
7+
public Randomizer(){
8+
9+
}
10+
11+
public static Random getInstance(){
12+
if(theInstance == null){
13+
theInstance = new Random();
14+
}
15+
return theInstance;
16+
}
17+
18+
/**
19+
* Return a random boolean value.
20+
* @return True or false value simulating a coin flip.
21+
*/
22+
public static boolean nextBoolean(){
23+
return Randomizer.getInstance().nextBoolean();
24+
}
25+
26+
/**
27+
* This method simulates a weighted coin flip which will return
28+
* true with the probability passed as a parameter.
29+
*
30+
* @param probability The probability that the method returns true, a value between 0 to 1 inclusive.
31+
* @return True or false value simulating a weighted coin flip.
32+
*/
33+
public static boolean nextBoolean(double probability){
34+
return Randomizer.nextDouble() < probability;
35+
}
36+
37+
/**
38+
* This method returns a random integer.
39+
* @return A random integer.
40+
*/
41+
public static int nextInt(){
42+
return Randomizer.getInstance().nextInt();
43+
}
44+
45+
/**
46+
* This method returns a random integer between 0 and n, exclusive.
47+
* @param n The maximum value for the range.
48+
* @return A random integer between 0 and n, exclusive.
49+
*/
50+
public static int nextInt(int n){
51+
return Randomizer.getInstance().nextInt(n);
52+
}
53+
54+
/**
55+
* Return a number between min and max, inclusive.
56+
* @param min The minimum integer value of the range, inclusive.
57+
* @param max The maximum integer value in the range, inclusive.
58+
* @return A random integer between min and max.
59+
*/
60+
public static int nextInt(int min, int max){
61+
return min + Randomizer.nextInt(max - min + 1);
62+
}
63+
64+
/**
65+
* Return a random double between 0 and 1.
66+
* @return A random double between 0 and 1.
67+
*/
68+
public static double nextDouble(){
69+
return Randomizer.getInstance().nextDouble();
70+
}
71+
72+
/**
73+
* Return a random double between min and max.
74+
* @param min The minimum double value in the range.
75+
* @param max The maximum double value in the rang.
76+
* @return A random double between min and max.
77+
*/
78+
public static double nextDouble(double min, double max){
79+
return min + (max - min) * Randomizer.nextDouble();
80+
}
81+
82+
83+
}

index.html

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ <h1 class="logo pull-left">
5151
<li class="nav-item"><a class="scrollto" href="#docs">Quick Start</a></li>
5252
<li class="nav-item"><a class="scrollto" href="#eclipse">Eclipse</a></li>
5353
<li class="nav-item"><a class="scrollto" href="#bluej">BlueJ</a></li>
54+
<li class="nav-item"><a class="scrollto" href="#terminal">Terminal</a></li>
5455
</ul><!--//nav-->
5556
</div><!--//navabr-collapse-->
5657
</nav><!--//main-nav-->
@@ -257,10 +258,10 @@ <h3 class="sub-title text-center">Quick ConsoleProgram HelloWorld in BlueJ</h3>
257258
<ol>
258259
<li>Download the zip of the <a href="HelloWorldBluejay.zip">Hello project.</a> and unzip it.</li>
259260
<li>Double click the package.bluej file to open the BlueJ project</li>
260-
<li>Right click on ConsoleProgram and select "void main(String[] args)"</li>
261+
<li>Right click on Hello and select "void main(String[] args)"</li>
261262
<li>Then as the arguments type {"Hello"}</li>
262263

263-
<img src="https://www.evernote.com/l/AATC9IIdGwlFGp9SPTu797Eq573OIoLWZHYB/image.png" width="600">
264+
<img src="https://www.evernote.com/l/AATTD_1yydpGp5BOJNJdhgFUocru7GBhYkYB/image.png" width="600">
264265
</ol>
265266
</div><!--//block-->
266267

@@ -273,6 +274,37 @@ <h3 class="sub-title text-center">Quick ConsoleProgram HelloWorld in BlueJ</h3>
273274
</section><!--//features-->
274275

275276

277+
<section id="terminal" class="docs section">
278+
<div class="container">
279+
<div class="docs-inner">
280+
<h2 class="title text-center">Running in the Terminal</h2>
281+
282+
<div class="block">
283+
<h3 class="sub-title text-center">Java HelloWorld in the Terminal</h3>
284+
<ol>
285+
<li>Download the zip of the <a href="HelloWorldTerminal.zip">Hello project</a> and unzip it.</li>
286+
<li>Save it to the Downloads folder. If you save it somewhere else then you will need to navigate
287+
there in the terminal using <code>ls</code> and <code>cd</code>.</li>
288+
<li>Open your terminal (on Macs the Terminal app)</li>
289+
<li>Change to the right directory. If you put it in Downloads it will be:
290+
<pre>cd Downloads/HelloWorldTerminal</pre>
291+
otherwise it will be in a different folder.
292+
<li>Compile the code with <pre>javac Hello.java</pre></li>
293+
<li>Run the code with <pre>java Hello Hello</pre> What's happening here is we are running the program
294+
called Hello and passing it as an argument the String "Hello" so that it knows which file to run.</li>
295+
296+
<img src="https://www.evernote.com/l/AATjorYeCmhDRrA79d3IhjMAxgn2Co-jUjYB/image.png" width="600">
297+
</ol>
298+
</div><!--//block-->
299+
300+
301+
302+
303+
304+
</div><!--//docs-inner-->
305+
</div><!--//container-->
306+
</section><!--//features-->
307+
276308
<!-- ******FOOTER****** -->
277309
<footer class="footer">
278310
<div class="container text-center">

0 commit comments

Comments
 (0)