Sequence Control in Jasmin

1. Tax Time

Write a console user interface (TaxCalculatorCUI.java) in Java that perpetually:

1. Prompts the user for his income
2. Reads the income
3. Determines the amount of income tax and prints the result
4. Asks the user if he/she wants to continue
5. Quits if the answer is no.

A separate TaxCalculator class determines the tax owed:

class TaxCalculator {
   public static float tax(float income) { ... }
}

Here's the (fictitious) algorithm:

income < $0 => error
$0 <= income < $5000 => tax = 5% of income
$5000 <= income < $15000 => tax = 8% of income
$15000 <= income < $25000 => tax = 10% of income
$25000 <= income < $35000 => tax = 12% of income
$35000 <= income < $50000 => tax = 15% of income
$50000 <= income < $75000 => tax = 20% of income
$75000 <= income => tax = 25% of income

Finish the implementation of TaxCalculator.java and test your program.

Translate TaxCalculator.java into TaxCalculator.j and re-test your program.

2. February 29

Write a console user interface (LeapYearCUI.java) in Java that perpetually:

1. Prompts the user for a year
2. Reads the year as an int (e.g. 1999)
3. Determines if the year is a leap-year and prints the result
4. Asks the user if he/she wants to continue
5. Quits if the answer is no.

A separate LeapYearCalculator class determines if a given year is a leap year or not:

class LeapYearCalculator {
   public static boolean isLeapYear(int y) { ... }
}

Here's the algorithm:

y > 1582 // = the year leap years were introduced
y is divisible by 4
but y is not divisible by 100
unless y is divisible by 400

For example, 1900 is not a leap year, but 2000 is a leap year.

Finish the implementation of LeapYearCalculator.java and test your program.

Translate LeapYearCalculator.java into LeapYearCalculator.j and re-test your program.

3. Classic Math

Write a console user interface (MathCUI.java) in Java that perpetually:

1. Displays a menu of functions (see below for the list)
2. Reads the user's choice
3. Quits if the user selected quit
4. Prompts for 1 or 2 longs as required
5. Applies the selected function to the longs
6. displays the result

A separate MathUtils class contains the actual implementations of the possible functions:

class MathUtils {
   /**
      = n! (done in class)
   */
   public static long fact(long n) { ... }
   /**
      = # of ways to choose m things from n
      = n!/((n – m)! * m!)
   */
   public static long choose(long n, long m) { ... }
   /**
      = base * base * ... * base (e times)
   */
   public static long exp(long base, long e) { ... }
   /**
      natural exponent
      = e^n
      = sum of x^i/i! for i <= n
      = x^0/0! + x^1/1! + x^2/2! + ... + x^n/n!
   */
   public static long natExp(long x, long n) { ... }
   /**
      = n + ... + m
      = 0 if m < n
   */
   public static long sum(long n, long m) { ... }
   /**
      = sum of squares of even numbers between n and m
      = 0 if M < n
   */
   public static long soes(long n, long m) { ... }
   /**
      = n-th Fibonacci number
      = fib(n – 1) + fib(n – 2) if n > 1
      = 0 if n = 0
      = 1 if n = 1
   */
   public static long fib(long n) { ... }
   /**
      = 1 if n is prime
      = 0 otherwise
   */
   public static int isPrime(int n) { ... }
}

Finish the implementation of MathUtils.java and test your program.

Translate MathUtils.java into MathUtils.j and re-test your program.

Hint: For some of these you will need to learn how to invoke static methods in Jasmin.