3 - Towson

advertisement
Assignment 2 (Due 03/11/2010)
COSC 237-101
Name: Ryan Stevens
ID:0283589
Email : rsteve1@students.towson.edu
Complete the following problems and submit your answers via email. All the program code must
be placed a word document (preferred) and submitted via Blackboard. Please put output of each
program in the word document.
Problem 2.1: Bank account balance
Please download the CheckingAccountBalance.java and money.txt from the blackboard and
make the program complete.
Hint: The complete source code is in pages 250-251.
Problem 2.2: Reversing Digit
Write a method, reverseDigit,that takes a positive integer as a parameter and returns the number
with its digits reversed. For example, the value of reverseDigit(12345) is 54321. Also write a
program to test it.
Hint: there are two ways to solve the problem: (1) You can use “/” and “%” operation to get the
reversed integer. (2) You use “int aInt = 80; String aString = Integer.toString(aInt);” to convert
the integer value to string; you use for loop to reverse the data in the string (look at example in
page 378); then you use the “String aString = "80"; int aInt = Integer.parseInt(aString);” to return
the integer data.
import java.util.Scanner;
import java.io.*;
public class Reverse {
public static void main (String args[]) {
Scanner in = new Scanner(System.in);
int n=0;
int rev_n = 0;
System.out.println("Enter your number");
n = in.nextInt();
while (n > 0) {
rev_n *= 10;
rev_n += n % 10;
n /= 10;
}
System.out.println("Enter your number reversed is " + rev_n);
}
}
Problem 2.3: Finding Duplications:
Write a program to sign people up for a seminar. Everybody is assigned a ID number in [0, 15].
It will start with a simple routine that asks users to type in their number. Once you have the array
of up to 15 numbers, you will want to check whether some people may have entered their
numbers twice or not. Write a program which shows the one of duplicate numbers?
Hint: To do this, you will want to iterate over the original array using loop. You will examine
each element that you encounter to see if it exists in the new array that you are building (as an
index array). We definitely have an alternative way to do this by using nested loops.
import java.util.Scanner;
import java.io.*;
public class seminar {
public static void main (String args[]) {
Scanner in = new Scanner(System.in);
int[] x = {1, 6, 3, 1, 2, 4, 5, 1, 3, 10};
int n=0;
int y=0;
System.out.println("Please enter your ID number");
n = in.nextInt();
for (int i= 0; i <x.length; i++)
if (n != x[i])
y = 1;
if (y==0)
System.out.println("This ID number is avavliable");
else
System.out.println("This ID number not is avavliable");
}
}
Download