Skip to content

Hello World not working #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
Binary file added Hello.zip
Binary file not shown.
Binary file added HelloWorldBluejay.zip
Binary file not shown.
Binary file added HelloWorldTerminal.zip
Binary file not shown.
Binary file added HelloWorldTerminal/ConsoleProgram.class
Binary file not shown.
125 changes: 125 additions & 0 deletions HelloWorldTerminal/ConsoleProgram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import java.util.Scanner;

public class ConsoleProgram{

private Scanner scanner;

public static void main(String[] args){
// Assume the class name is passed in as the first argument.

if(args.length == 0){
System.out.println("Please provide the name of the main class as an argument.");
return;
}

String mainClassName = args[0];

try{
Class mainClass = Class.forName(mainClassName);
Object obj = mainClass.newInstance();
ConsoleProgram program = (ConsoleProgram)obj;
program.run();
} catch (IllegalAccessException ex) {
System.out.println("Error in program. Make sure you extend ConsoleProgram");
} catch (InstantiationException ex) {
System.out.println("Error in program. Make sure you extend ConsoleProgram");
} catch (ClassNotFoundException ex) {
System.out.println("Error in program. Make sure you extend ConsoleProgram");
}
}

/**
* The run method is overwritten by the subclass. This is the main entry point for
* ConsolePrograms. There is no main method in student programs that extend ConsoleProgram,
* instead they write their application starting like this:
*
* public void run()
* {
* // student code here
* }
*
* The main method of ConsoleProgram calls this run method when it begins.
*/
public void run(){
/* Overridden by subclass */
}

/**
* Constructor for Console program. Create a scanner instance to be used for user input.
*/
public ConsoleProgram(){
scanner = new Scanner(System.in);
}

/**
* This method reads a line of input from the user, given a prompt.
*
* @param prompt A prompt to the user to get input
* @return A String of the user input.
*/
public String readLine(String prompt){
System.out.print(prompt);
return scanner.nextLine();
}

/**
* This method reads a boolean value from the user, asking them for either
* a true or false value. This makes use of the readLine method.
*
* @param prompt A prompt to the user to read a boolean value
* @return A boolean value read from the user.
*/
public boolean readBoolean(String prompt){

while(true){
String input = readLine(prompt);

if(input.equalsIgnoreCase("true")){
return true;
}

if(input.equalsIgnoreCase("false")){
return false;
}
}
}

/**
* This method reads a double value from the user, given a prompt.
*
* @param prompt A prompt to the user to ask for a a double.
* @return A double value read from the user.
*/
public double readDouble(String prompt){

while(true){
String input = readLine(prompt);
try {
double n = Double.valueOf(input).doubleValue();
return n;
} catch (NumberFormatException e){

}
}
}

/**
* This method reads a int value from the user, given a prompt.
*
* @param prompt A prompt to the user to ask for an int
* @return The int value read from the user.
*/
public int readInt(String prompt){

while(true){
String input = readLine(prompt);
try {
int n = Integer.parseInt(input);
return n;
} catch (NumberFormatException e){

}
}
}

}
Binary file added HelloWorldTerminal/Hello.class
Binary file not shown.
7 changes: 7 additions & 0 deletions HelloWorldTerminal/Hello.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class Hello extends ConsoleProgram
{
public void run()
{
System.out.println("Hello World!");
}
}
83 changes: 83 additions & 0 deletions HelloWorldTerminal/Randomizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import java.util.Random;

public class Randomizer{

public static Random theInstance = null;

public Randomizer(){

}

public static Random getInstance(){
if(theInstance == null){
theInstance = new Random();
}
return theInstance;
}

/**
* Return a random boolean value.
* @return True or false value simulating a coin flip.
*/
public static boolean nextBoolean(){
return Randomizer.getInstance().nextBoolean();
}

/**
* This method simulates a weighted coin flip which will return
* true with the probability passed as a parameter.
*
* @param probability The probability that the method returns true, a value between 0 to 1 inclusive.
* @return True or false value simulating a weighted coin flip.
*/
public static boolean nextBoolean(double probability){
return Randomizer.nextDouble() < probability;
}

/**
* This method returns a random integer.
* @return A random integer.
*/
public static int nextInt(){
return Randomizer.getInstance().nextInt();
}

/**
* This method returns a random integer between 0 and n, exclusive.
* @param n The maximum value for the range.
* @return A random integer between 0 and n, exclusive.
*/
public static int nextInt(int n){
return Randomizer.getInstance().nextInt(n);
}

/**
* Return a number between min and max, inclusive.
* @param min The minimum integer value of the range, inclusive.
* @param max The maximum integer value in the range, inclusive.
* @return A random integer between min and max.
*/
public static int nextInt(int min, int max){
return min + Randomizer.nextInt(max - min + 1);
}

/**
* Return a random double between 0 and 1.
* @return A random double between 0 and 1.
*/
public static double nextDouble(){
return Randomizer.getInstance().nextDouble();
}

/**
* Return a random double between min and max.
* @param min The minimum double value in the range.
* @param max The maximum double value in the rang.
* @return A random double between min and max.
*/
public static double nextDouble(double min, double max){
return min + (max - min) * Randomizer.nextDouble();
}


}
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# codehs-java-library


How to use:

* Always stay on and push to the gh-pages branch.
* The public site lives at: http://codehs.github.io/codehs-java-library
* For Jeremy, the Eclipse project with the files lives in CodeHSLibrary
* In Eclipse, generate the latest Javadocs with Project/Generate Javadoc command
* Copy over and replace the doc and src fodl
Loading