Skip to content
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

Primitive Data Types: Jshort #35

Open
nsanitate opened this issue Aug 3, 2017 · 0 comments
Open

Primitive Data Types: Jshort #35

nsanitate opened this issue Aug 3, 2017 · 0 comments

Comments

@nsanitate
Copy link
Member

Implement the short primitive data type that allow to overcome the differences between Java and JavaScript.

The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).

Expected Behavior

  • Declaring a variable to refer to a short
  • Creating, Initializing, and Accessing a short
    • initialize with hexadecimal
    • initialize with binary
  • Equality and Relational Operators
    • equal to
    • not equal to
    • greater than
    • greater than or equal to
    • less than
    • less than or equal to
  • Arithmetic Operators
    • additive operator
    • subtraction operator
    • multiplication operator
    • division operator
    • remainder operator
  • Unary Operators
    • unary plus operator
    • unary minus operator
    • increment operator
    • decrement operator
  • Exception
    • incompatible types: possible lossy conversion with int, long, float o double
    • incompatible types: cannot be converted to short

Acceptance test

Declaring a variable to refer to a short

let aShort: Jshort;

Creating, Initializing, and Accessing a short

const anShort = jshort('26');
console.log("Short: " + aShort);

// initialize with hexadecimal
const anHex = jshort('0x1a');
console.log("Hex: " + anHex);

// initialize with binary
const aBin = jshort('0b11010');
console.log("Bin: " + aBin);

Output:

Short: 26
Hex: 26
Bin: 26

Equality and Relational Operators

const value1 = jshort('1');
const value2 = jshort('2');

if(is(value1.eq(value2)))
   console.log("value1 == value2");

if(is(value1.ne(value2)))
   console.log("value1 != value2");

if(is(value1.gt(value2)))
   console.log("value1 > value2");

if(is(value1.ge(value2)))
   console.log("value1 > value2");

if(is(value1.lt(value2)))
   console.log("value1 < value2");

if(is(value1.le(value2)))
   console.log("value1 <= value2");

Output:

value1 != value2
value1 <  value2
value1 <= value2

Arithmetic Operators

const value8 = jshort('8');
const value7 = jshort('7');
let result: Jshort;

result = value8.add(value7);
console.log("8 + 7 = " + result);

result = value8.sub(value7);
console.log("8 - 7 = " + result);

result = value8.mul(value7);
console.log("8 * 7 = " + result);

result = value8.div(value7);
console.log("8 / 7 = " + result);

result = value8.mod(value7);
console.log("8 % 7 = " + result);

Output:

8 + 7 = 15
8 - 7 = 1
8 * 7 = 56
8 / 7 = 1
8 % 7 = 1

Unary Operators

const value1 = jshort('-1');
let result: Jshort;

result = value1.plus();
console.log("+(-1) = " + result);

value1.inc();
console.log("++(-1) = " + value1);

value1.dec();
console.log("--(0) = " + value1);

result = value1.minus();
console.log("-(-1) = " + result);

Output:

+(-1) = -1
++(-1) = 0
--(0) = -1
-(-1) = 1

Exceptions

const aShort = jshort('32768');
const anotherShort = jshort('ten');

Output:

ERROR: incompatible types: possible lossy conversion from int to short
ERROR: incompatible types: String cannot be converted to short
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant