Monday, September 14, 2009

HW1 example

import java.util.Scanner;
public class HW1Demo {

    /**
     * report bugs to xusun09@gmail.com
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Please enter the first 9 digits of the ISBN number:");
       
        Scanner keyboard = new Scanner (System.in);
        String number = keyboard.nextLine();
        number = number.replaceAll("-","");
       
        int first   = Integer.parseInt(number.substring(0, 1));
        int second  = Integer.parseInt(number.substring(1, 2));
        int third   = Integer.parseInt(number.substring(2, 3));
        int fourth  = Integer.parseInt(number.substring(3, 4));
        int fifth   = Integer.parseInt(number.substring(4, 5));
        int sixth   = Integer.parseInt(number.substring(5, 6));
        int seventh = Integer.parseInt(number.substring(6, 7));
        int eighth  = Integer.parseInt(number.substring(7, 8));
        int ninth   = Integer.parseInt(number.substring(8, 9));
           
        int tenth = 11 - (10*first + 9*second + 8*third + 7*fourth + 6*fifth + 
                    5*sixth + 4*seventh + 3*eighth + 2*ninth)%11;
           
        System.out.println("The last digit is: " + tenth);
    }
}

Thursday, September 10, 2009

lab 05 using only 2 comparisons

import java.util.Scanner;
public class lab05demo {
 /**
  * Another solution I mentioned in the lab
  * xusun09@gmail.com
  */
 public static void main(String[] args) {

  Scanner reader = new Scanner(System.in);
  String s1 = reader.next();
  String s2 = reader.next();
  String s3 = reader.next();
  
  if ((s1.compareTo(s2)>=0&&0>=s1.compareTo(s3))||(s1.compareTo(s2)<=0&&0<=s1.compareTo(s3)))
  {
   System.out.println(s1);
  }
  else if ((0>=s1.compareTo(s2)&&s1.compareTo(s2)>=s1.compareTo(s3))||
           (0<=s1.compareTo(s2)&&s1.compareTo(s2)<=s1.compareTo(s3)))
  {
   System.out.println(s2);
  }
  else System.out.println(s3);
 }
}

Wednesday, September 9, 2009

Lab 06

Question 8 on page 172. The option part is not required.

Tuesday, September 8, 2009

Chapter 4 Lecture

Below is the screencast for the Chapter 4 lecture. It is on loops: while and for loops. Every single programming language has while and for loops, and they always named 'while' and 'for', although the specific syntax of the for-loops can change a little bit between programming languages. You need to have read Chapter 4 and watched the screencast before class on Tuesday, September 15.

CSCE 145: Chapter 4 from Jose Vidal on Vimeo.

Here are the slides from the textbook, for your reviewing needs:

Homework 2: Taxes

For this homework you will implement a program that calculates the federal taxes that a single person in the US must pay given her gross income. This homework is due Tuesday September 15 @noon. Like many countries, the US uses tax brackets to calculate the taxes a person must pay. The steps to calculate the tax amount are the following: (also see the wikipedia explanation)

  1. Start with the gross income, subtract $5,700 from it. This is the new income value.
  2. Use the table below and apply the various marginal tax rates depending on the person's new income value:
    IncomeMarginal Tax Rate
    <= $835010%
    $8,351– $33,95015%
    $33,951 – $82,25025%
    $82,251 – $171,55028%
    $171,551 – $372,95033%
    $372,951+35%

For example, if the income is $20,000, then we first subtract $5700 to get $14,300. Of this we have to pay 10% for the first $8,350, which works out to be $835, plus 15% of ($14,300 - $8,350) which is $892. So the total tax is $835 + $892 = $1,727.

So, your program will ask the user to enter the income and then print out the tax, as such:

Please enter income: 20000
Your taxes are $1727.

Please think carefully about this program before you start coding it. Remember, the homework is due Tuesday September 15 @noon. Please email it to your TA when you are done.

lab 05

Question 3 on page 172. Have fun. =)

Thursday, September 3, 2009

Chapter 3 Lecture

Below is the screencast for Chapter 3 of the textbook. You should watch this lecture and read Chapter 3 before lecture on Tuesday, September 8. This chapter introduces loops: while and for loops. These are the most basic looping constructs and every programming language has them.

CSCE 145: Chapter 3 from Jose Vidal on Vimeo.

For your review, here are the textbook slides for chapter 3.

Lab 04

Question 13 on page 118. Notes: you do NOT need to use JOptionPane I/O, console input/output will be fine. Have fun! =)

Tuesday, September 1, 2009

Homework 1: ISBN check digits

Every book has an ISBN number. It is that number you see just above the bar code. For example, our textbook has an ISBN of 013607225-9. The dashes are not important, only the numbers matter.

Most importantly, the last number, known as a check digit, is computed from the first 10 digits using a specific equation. The wikipedia explains how:

A check digit is a form of redundancy check used for error detection, the decimal equivalent of a binary checksum. It consists of a single digit computed from the other digits in the message.

ISBN-10

The 2001 edition of the official manual of the International ISBN Agency says that the ISBN-10 check digit — which is the last digit of the ten-digit ISBN — must range from 0 to 10 (the symbol X is used instead of 10) and must be such that the sum of all the ten digits, each multiplied by the integer weight, descending from 10 to 1, is a multiple of the number 11. Modular arithmetic is convenient for calculating the check digit using modulus 11. Each of the first nine digits of the ten-digit ISBN — excluding the check digit, itself — is multiplied by a number in a sequence from 10 to 2, and the remainder of the sum, with respect to 11, is computed. The resulting remainder, plus the check digit, must equal 11; therefore, the check digit is 11 minus the remainder of the sum of the products.

For example, the check digit for an ISBN-10 of 0-306-40615-? is calculated as follows:

s = 0×10 + 3×9 + 0×8 + 6×7 + 4×6 + 0×5 + 6×4 + 1×3 + 5×2
  =    0 +  27 +   0 +  42 +  24 +   0 +  24 +   3 +  10
  = 130
130 / 11 = 11 remainder 9
11 - 9 = 2
Thus, the check digit is 2, and the complete sequence is ISBN 0-306-40615-2.

Formally, the check digit calculation is:
If the result is 11, a '0' should be substituted; if 10, an 'X' should be used.

The two most common errors in handling an ISBN (e.g., typing or writing it) are an altered digit or the transposition of adjacent digits. Since 11 is a prime number, the ISBN check digit method ensures that these two errors will always be detected. However, if the error occurs in the publishing house and goes undetected, the book will be issued with an invalid ISBN.

For this homework you will write a program which asks the users for the first 9 digits of the ISBN-10 number and then calculates and prints out the last digit. As such:

Enter 9-digit number: 0-13-607225
The last digit is 9
or
Enter 9-digit number: 013607225
The last digit is 9

Notice that your program must work even if the user enters the number with dashes, which he can place anywhere he wants.

The program is due by Tuesday, Sept. 8 @noon. When you are finished simply email the .java file to your TA, make sure you type CSCE145 in the Subject: line of your email. You should also cc: me in that email. The email must be sent by the deadline or you will not receive credit.

Finally, remember that you are bound by our Honor Code. Please, be responsible for your own work.

Lab 03

Question 4 on page 116. Have fun!