Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@6167656e74323431
Last active October 4, 2019 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 6167656e74323431/3196d2929c1ab86b4a55beefba5bbe7b to your computer and use it in GitHub Desktop.
Save 6167656e74323431/3196d2929c1ab86b4a55beefba5bbe7b to your computer and use it in GitHub Desktop.
ICS3U In-Class Contest Template
ICS3U In-Class Contest Template

ICS3U In-Class Contest Template

This is a template for students who are writing the ICS3U contests on MCPT's online judge. It allow them to get input in a similar manner to how it it taught in class via HSA Console by creating a wrapper class that emulates its core features.

Guide

  • In README.md (this file) you can find an introduction to this project, a basic usage guide, and the software license.
  • In DOCUMENTATION.md you can find a more in-depth explanation of all the functions emulated by the attached software.
  • In Template.java you can find the actual wrapper class, as well as a class that initializes an instance of the console to use as a base for your problem submissions.
  • In TemplateCompact.java you can find the actual wrapper class, with most of the whitespace removed, as well as a class that initializes an instance of the console to use as a base for your problem submissions.
  • In TriangleArea.java you can find an example solution to a problem on MCPT's online judge making use of the Template and the Console classes.
  • .ICS3U In-Class Contest Template just exists to set the name of this gist to something other than README.md.

Basic Usage

To use the full functionality of the console class simply paste it in under your current class if you have one already. Alternatively, if you haven't started programming yet, you can copy the entire file and write your code in place of /****** Your Code Here ******/. Note: you can change the name of the Template class to whatever you'd like provided you rename the file accordingly.

License

MIT License

Copyright (c) 2019 Theodore Preduta

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

In this file you will find every method included in the Console wrapper class.

The Console wrapper class only contains some of the core functionality of the hsa.Console class. As such, many features, have been left out. Most notably all graphics functions have been left out. In addition to this formatted print statements (such as ones with special perameters for minimum column width, and number of decimal places) have been left out.

Console Constructor

public Console() The constructor initializes the classes used to do input and output.

close Method

public void close() The close method closes the classes that are used to take the input.

getChar Method

public char getChar() The getChar method reads a single character from the STDIN stream.

Returns:

The character read from the STDIN stream.

print Method

public <T> void print(T x) The print method outputs the value passed to the parameter to the STDOUT stream.

Parameters

x - the object to be written to the STDOUT stream.

println Method

public void println() The println method outputs a newline character to the STDOUT stream.

println Method

public <T> void println(T x) The println method outputs the value passed to the parameter to the STDOUT stream, followed by a newline character.

Parameters

x - the object to be written to the STDOUT stream.

readBoolean Method

public boolean readBoolean() The readBoolean method reads a boolean (either true of false, not case-sensitive) from the STDIN stream.

Returns:

The boolean read from the STDIN stream.

readByte Method

public byte readByte() The readByte method reads an 8-bit integer (-128 to 127 inclusive) from the STDIN stream.

Returns:

The integer read from the STDIN stream.

readChar Method

public char readChar() The readChar method reads a character from the STDIN stream. Note: it is recommended that you use the getChar method instead of this one.

Returns:

The character read from the STDIN stream.

readDouble Method

public double readDouble() The readDouble method reads a double precision floating point number from the STDIN stream.

Returns:

The double read from the STDIN stream.

readFloat Method

public float readFloat() The readFloat method reads a floating point number from the STDIN stream.

Returns:

The float read from the STDIN stream.

readInt Method

public int readInt() The readInt method reads a 32-bit integer (-2147483648 to 2147483647 inclusive) from the STDIN stream.

Returns:

The integer read from the STDIN stream.

readLine Method

public java.lang.String readLine() The readLine method reads a full line of text from the STDIN stream.

Returns:

The line of text read from the STDIN stream.

readLong Method

public long readLong() The readLong method reads a 64-bit integer (-9223372036854775808 to 9223372036854775807 inclusive) from the STDIN stream.

Returns:

The long read from the STDIN stream.

readShort Method

public short readShort() The readShort method reads a 16-bit integer (-32768 to 32767 inclusive) from the STDIN stream.

Returns:

The short read from the STDIN stream.

readString Method

public java.lang.String readString() The readString method reads a whitespace delimited token from the STDIN stream. Note: this is identical to the readToken method.

Returns:

The token read from the STDIN stream.

readToken Method

public java.lang.String readToken() The readToken method reads a whitespace delimited token from the STDIN stream. Note: this is identical to the readString method.

Returns:

The token read from the STDIN stream.

/** Place all your code inside this class. */
public class Template
{
/* Variable used to store a Console instance */
private static Console c;
public static void main(String[] args)
{
/* Initializes the console variable */
c = new Console();
/****** Your Code Here ******/
/* Closes the console */
c.close();
}
}
/** A wrapper class that emulates the hsa.Console class. */
class Console
{
/** Scanner is used to take input from STDIN. */
private java.util.Scanner s;
/** Initializes the Scanner class used for input. */
public Console()
{ s = new java.util.Scanner(System.in); }
/** Closes the Scanner class stream. */
public void close()
{ s.close(); }
/** Reads a single character. */
public char getChar()
{ return s.findInLine(".").charAt(0); }
/** Prints the given parameter. */
public <T> void print(T x)
{ System.out.print(x); }
/** Prints a newline character. */
public void println()
{ System.out.println(); }
/** Prints the given parameter and adds a newline character. */
public <T> void println(T x)
{ System.out.println(x); }
/** Reads a boolean (either "true" or "false"). */
public boolean readBoolean()
{ return s.nextBoolean(); }
/** Reads an 8-bit integer (-128 to 127). */
public byte readByte()
{ return s.nextByte(); }
/** Reads a single character. It's HIGHLY RECOMENDED that you use getChar() instead. */
public char readChar()
{ return s.next(".").charAt(0); }
/** Reads a double precision floating point number. */
public double readDouble()
{ return s.nextDouble(); }
/** Reads a floating point number. */
public float readFloat()
{ return s.nextFloat(); }
/** Reads a 32-bit integer (-2147483648 to 2147483647). */
public int readInt()
{ return s.nextInt(); }
/** Reads a full line of text. */
public java.lang.String readLine()
{ return s.nextLine(); }
/** Reads a 64-bit integer (-9223372036854775808 to 9223372036854775807). */
public long readLong()
{ return s.nextLong(); }
/** Reads a 16-bit integer (-32768 to 32767). */
public short readShort()
{ return s.nextShort(); }
/** Reads a token, delimited by any whitespace. */
public java.lang.String readString()
{ return s.next(); }
/** Reads a token, delimited by any whitespace. */
public java.lang.String readToken()
{ return s.next(); }
}
/** Place all your code inside this class. */
public class TemplateCompact
{
/* Variable used to store a Console instance */
private static Console c;
public static void main(String[] args)
{
/* Initializes the console variable */
c = new Console();
/****** Your Code Here ******/
/* Closes the console */
c.close();
}
}
/** A wrapper class that emulates the hsa.Console class. */
class Console { private java.util.Scanner s;
public Console() { s = new java.util.Scanner(System.in); }
public void close() { s.close(); }
public char getChar() { return s.findInLine(".").charAt(0); }
public <T> void print(T x) { System.out.print(x); }
public void println() { System.out.println(); }
public <T> void println(T x) { System.out.println(x); }
public boolean readBoolean() { return s.nextBoolean(); }
public byte readByte() { return s.nextByte(); }
public char readChar() { return s.next(".").charAt(0); }
public double readDouble() { return s.nextDouble(); }
public float readFloat() { return s.nextFloat(); }
public int readInt() { return s.nextInt(); }
public java.lang.String readLine() { return s.nextLine(); }
public long readLong() { return s.nextLong(); }
public short readShort() { return s.nextShort(); }
public java.lang.String readString() { return s.next(); }
public java.lang.String readToken() { return s.next(); } }
/** Class that solves the triangle area problem on mcpt's online judge */
/** problem found at https://mcpt.ca/problem/trianglearea */
public class TriangleArea
{
private static Console c;
public static void main(String[] args)
{
/* Initializes the console variable */
c = new Console();
/* Declare a variable for each side of the triangle */
int x, y, z;
/* Get the values of the 3 sides */
x = c.readInt();
y = c.readInt();
z = c.readInt();
/* Calculate the semi-perimieter of the triangle */
double s = (x + y + z)/2.0;
/* Calculate the area using Heron's Formula (https://en.wikipedia.org/wiki/Heron%27s_formula) */
double area = Math.sqrt(s * (s - x) * (s - y) * (s - z));
/* Round the area to the nearest integer */
int roundedArea = (int)Math.round(area);
/* Output the area of the triangle */
c.print(roundedArea);
/* Closes the console */
c.close();
}
}
/** A wrapper class that emulates the hsa.Console class. */
class Console
{
/** Scanner is used to take input from STDIN. */
private java.util.Scanner s;
/** Initializes the Scanner class used for input. */
public Console()
{ s = new java.util.Scanner(System.in); }
/** Closes the Scanner class stream. */
public void close()
{ s.close(); }
/** Reads a single character. */
public char getChar()
{ return s.findInLine(".").charAt(0); }
/** Prints the given parameter. */
public <T> void print(T x)
{ System.out.print(x); }
/** Prints a newline character. */
public void println()
{ System.out.println(); }
/** Prints the given parameter and adds a newline character. */
public <T> void println(T x)
{ System.out.println(x); }
/** Reads a boolean (either "true" or "false"). */
public boolean readBoolean()
{ return s.nextBoolean(); }
/** Reads an 8-bit integer (-128 to 127). */
public byte readByte()
{ return s.nextByte(); }
/** Reads a single character. It's HIGHLY RECOMENDED that you use getChar() instead. */
public char readChar()
{ return s.next(".").charAt(0); }
/** Reads a double precision floating point number. */
public double readDouble()
{ return s.nextDouble(); }
/** Reads a floating point number. */
public float readFloat()
{ return s.nextFloat(); }
/** Reads a 32-bit integer (-2147483648 to 2147483647). */
public int readInt()
{ return s.nextInt(); }
/** Reads a full line of text. */
public java.lang.String readLine()
{ return s.nextLine(); }
/** Reads a 64-bit integer (-9223372036854775808 to 9223372036854775807). */
public long readLong()
{ return s.nextLong(); }
/** Reads a 16-bit integer (-32768 to 32767). */
public short readShort()
{ return s.nextShort(); }
/** Reads a token, delimited by any whitespace. */
public java.lang.String readString()
{ return s.next(); }
/** Reads a token, delimited by any whitespace. */
public java.lang.String readToken()
{ return s.next(); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment