Skip to content
Snippets Groups Projects
Commit 0440b8cb authored by ik1g19's avatar ik1g19
Browse files

add labs

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 390 additions and 0 deletions
File added
public class Hello {
//an error was caused here as I put the square brackets in front of String, rather than args
public static void main(String[] args){
//An error was caused here as the L in println was capitalised
//An error was also caused here because of a missing semicolon at the end
//this line prints out the message Hellow World!
System.out.println("Hello World!");
}
}
\ No newline at end of file
File added
import static org.junit.jupiter.api.Assertions.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
class OutputCapturer {
private PrintStream origOut;
private ByteArrayOutputStream outputStream;
public void start()
{
this.origOut = System.out;
this.outputStream = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(this.outputStream);
System.setOut(ps);
}
public String getOutput() {
System.out.flush();
return this.outputStream.toString().replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
}
public void stop() {
System.setOut(this.origOut);
}
}
@DisplayName("Lab 1 part 1")
class HelloTest {
OutputCapturer outputHarness;
@BeforeEach
public void setup() {
this.outputHarness = new OutputCapturer();
this.outputHarness.start();
}
@AfterEach
public void tearDown() {
this.outputHarness.stop();
}
@Test
@DisplayName("Test printing of \"Hello World!\"")
public void testMain(){
try {
Hello.main(null);
} catch (Exception e) {
fail("threw an exception");
}
String output = outputHarness.getOutput();
String expected = "Hello World!";
assertNotNull(output, "Tests that there is something printed to the terminal");
assertTrue(output.startsWith(expected), "Tests that \"Hello World!\" has printed");
}
}
File added
@SET JAVA_HOME=C:\Program Files\Java
@FOR /F %%G IN ('DIR /B "%JAVA_HOME%\JDK*"') DO @SET JDK_HOME=%JAVA_HOME%\%%G
@SET PATH=%JDK_HOME%\bin;%PATH%
@javac -version
@echo.
@echo %JDK_HOME%\bin successfully added to Windows PATH
@echo.
@echo Now type 'javac'.
@echo.
@echo.
@echo.
@CMD
\ No newline at end of file
File added
File added
public class GuessingGame
{
public static void main(String[] args){
//declaring the number to guess and the guessed number
Integer numberToGuess;
Integer guessedNumber;
Toolbox myToolbox = new Toolbox();
//welcoming the user, generating a random number and reading user input
System.out.println("Welcome to the number guessing game!");
numberToGuess = myToolbox.getRandomInteger(10);
guessedNumber = myToolbox.readIntegerFromCmd();
//checking if the users guess is correct
if (guessedNumber > numberToGuess) {
System.out.println("too high");
}
else if (guessedNumber < numberToGuess) {
System.out.println("too low");
}
else if (numberToGuess.equals(guessedNumber)) {
System.out.println("right");
}
}
}
\ No newline at end of file
File added
import static org.junit.jupiter.api.Assertions.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
class OutputCapturer {
private PrintStream origOut;
private ByteArrayOutputStream outputStream;
public void start()
{
this.origOut = System.out;
this.outputStream = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(this.outputStream);
System.setOut(ps);
}
public String getOutput() {
System.out.flush();
return this.outputStream.toString().replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
}
public void stop() {
System.setOut(this.origOut);
}
}
@DisplayName("Lab 1 part 2")
class GuessingGameTest {
OutputCapturer outputHarness;
@BeforeEach
public void setup() {
this.outputHarness = new OutputCapturer();
this.outputHarness.start();
}
@AfterEach
public void tearDown() {
this.outputHarness.stop();
}
@Test
@DisplayName("Tests when the guess was equal")
public void testEqual() {
Integer[] randomNumbers = { new Integer(5) };
String[] inputs = { new String("5") };
Toolbox.setTestingData(randomNumbers, inputs);
GuessingGame.main(null);
String out = outputHarness.getOutput();
assertTrue(out.toLowerCase().contains("right"), "Should say guess was right");
assertFalse(out.toLowerCase().contains("low"), "Should not say low");
assertFalse(out.toLowerCase().contains("high"), "Should not say high");
}
@Test
@DisplayName("Tests when the guess was too low")
public void testLow() {
Integer[] randomNumbers = { new Integer(5) };
String[] inputs = { new String("1") };
Toolbox.setTestingData(randomNumbers, inputs);
GuessingGame.main(null);
String out = outputHarness.getOutput();
assertFalse(out.toLowerCase().contains("right"), "Should not say right");
assertTrue(out.toLowerCase().contains("low"), "Should say low");
assertFalse(out.toLowerCase().contains("high"), "Should not say high"); }
@Test
@DisplayName("Tests when the guess was too high")
public void testHigh() {
Integer[] randomNumbers = { new Integer(5) };
String[] inputs = { new String("10") };
Toolbox.setTestingData(randomNumbers, inputs);
GuessingGame.main(null);
String out = outputHarness.getOutput();
assertFalse(out.toLowerCase().contains("right"), "Should not say right");
assertFalse(out.toLowerCase().contains("low"), "Should not say low");
assertTrue(out.toLowerCase().contains("high"), "Should say high");
}
}
File added
File added
/* To use Toolbox.java, copy and paste this code into a file called Toolbox.java
* and save it in the directory with the rest of your code.
*/
import java.io.*;
import java.lang.Integer;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Random;
public class Toolbox
{
private static Boolean testing = false;
private static Integer[] fakeRandomNumbers;
private static String[] fakeInputValues;
private ArrayDeque<Integer> randomValues;
private ArrayDeque<String> inputValues;
Toolbox() {
if (testing) {
this.randomValues = new ArrayDeque<Integer>(Arrays.asList(fakeRandomNumbers));
this.inputValues = new ArrayDeque<String>(Arrays.asList(fakeInputValues));
}
this.testing = testing;
}
public static void setTestingData(Integer[] randomValues, String[] inputValues) {
testing = true;
fakeRandomNumbers = randomValues;
fakeInputValues = inputValues;
}
public Integer getRandomInteger(Integer max)
{
if (this.testing) {
return this.randomValues.poll();
}
Random rand = new Random();
int number;
number = rand.nextInt(max) + 1;
return new Integer(number);
}
public Integer readIntegerFromCmd()
{
String number = null;
if (this.testing) {
System.out.println("Enter your number");
number = this.inputValues.poll();
} else {
System.out.println("Enter your number");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
number = br.readLine();
}
catch (IOException ioe)
{
System.err.println("There was an input error");
}
}
try
{
return new Integer(number);
}
catch (NumberFormatException e)
{
System.err
.println("There is something wrong with the number you entered");
}
return 0;
}
public String readStringFromCmd()
{
if (this.testing) {
System.out.println("Enter your String");
return this.inputValues.poll();
}
System.out.println("Enter your String");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String string;
string = null;
try
{
string = br.readLine();
}
catch (IOException ioe)
{
System.err.println("There was an input error");
}
return string;
}
}
File added
File added
//creating a wrapper for the class
public class FizzBuzz {
//creating a wrapper for the main class
public static void main(String[] args) {
//creating a for loop to loop through every integer between 1 and 61
for(Integer i = new Integer(1); i < 61; i++){
//if the number is divisible by 3
if(i % 3 == 0){
//then print Fizz
System.out.print("Fizz");
}
//if the number is divisible by 5
if(i % 5 == 0){
//then print Buzz
System.out.print("Buzz");
}
//if the number is neither divisble by 5 or 3
if(i % 3 != 0 && i % 5 != 0){
//then print the current number
System.out.print(i);
}
//start a new line
System.out.println();
}
}
}
\ No newline at end of file
File added
public class Hello {
//an error was caused here as I put the square brackets in front of String, rather than args
public static void main(String[] args){
//An error was caused here as the L in println was capitalised
//An error was also caused here because of a missing semicolon at the end
//this line prints out the message Hellow World!
System.out.println("Hello World!");
}
}
\ No newline at end of file
public class GuessingGame
{
public static void main(String[] args){
//declaring the number to guess and the guessed number
Integer numberToGuess;
Integer guessedNumber;
Toolbox myToolbox = new Toolbox();
//welcoming the user, generating a random number and reading user input
System.out.println("Welcome to the number guessing game!");
numberToGuess = myToolbox.getRandomInteger(10);
guessedNumber = myToolbox.readIntegerFromCmd();
//checking if the users guess is correct
if (guessedNumber > numberToGuess) {
System.out.println("too high");
}
else if (guessedNumber < numberToGuess) {
System.out.println("too low");
}
else if (numberToGuess.equals(guessedNumber)) {
System.out.println("right");
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment