Skip to content

Ahmet Faruk Bişkinler




CENG102Homework2Q2.java PDF Yazdır e-Posta
(0 votes, average 0 out of 5)
Lessons
Administrator tarafından yazıldı.   
Pazar, 03 Aralık 2006 22:19
Etiketler:

 

CENG 102 Assignment #2

 

Assigned: 19/10/05, Wednesday

Due: 26/10/05, Wednesday at 14:00

 

Homeworks must be delivered at the deadline date and hour.

 

(15 points)

Write a CheckBook and Check class with the following properties:

Consider the Check class. Each object in this class has the following attributes that are not visible to the user:

an int: number - giving the check number.

a String: name - the name of the person to whom the check is being written out to.

a String: date - the date the check was written (stored as a String).

a double: amount - the amount of the check.

The class has two constructors:

·         a constructor for blank checks that has a String parameter for the date and a CheckBook type parameter. The constructor increments the numChecks instance variable of the CheckBook object which is explained below.

·         a constructor for typical checks that has two String parameters, a double parameter, and a CheckBook type parameter defining the name of person to whom the check is being written to, the date the check was written, the amount the check is made out to and the CheckBook object that will be used in the same way described for the first constructor.

The class has the following accessor methods:

·         getName() - returns the name on the check.

·         getAmount() - returns the amount on the check.

·         getDate() - returns the date the check was written.

The class also has the following mutator method:

·         setName(String s) - changes the name on the check to s.

The CheckBook class keeps track of the number of checks that have been written (i.e., each time that a Check constructor is used) using an int called numChecks that is not visible to the user. Every time a check is written (a Check object is instantiated), the integer numChecks is incremented (its initial value is 0) and the attribute number for the new check is assigned the new value of numChecks. The CheckBook class should have the proper constructor.

Finally, the CheckBook class has a class method called getNumChecks() that returns the number of checks instantiated. Write a CheckTest class that demonstrates the use of the Check class as detailed as possible. You can add other necessary methods to the classes.

Check.java
  1.  
  2.  
  3. /*************************************/
  4. /* */
  5. /* AHMET FARUK BİŞKİNLER */
  6. /* */
  7. /* CENG 102 JAVA PROGRAMMING */
  8. /* */
  9. /* LESSON */
  10. /* */
  11. /* COMPUTER ENGINEERING 07010441 */
  12. /* */
  13. /* COPYRIGHT © 2003-2005 */
  14. /* by */
  15. /* AHMET FARUK BİŞKİNLER */
  16. /* */
  17. /* */
  18. /* http://ahbis.blogspot.com */
  19. /* http://afb.picom.com.tr */
  20. /* */
  21. /* All Rights Reserved */
  22. /* */
  23. /*************************************/
  24.  
  25. public class Check {
  26.  
  27. private int numberCheck; // giving the check number.
  28. private String nameCheck; // the name of the person to whom the check is being written out to.
  29. private String dateCheck; // the date the check was written (stored as a String).
  30. private double amountCheck; // the amount of the check.
  31.  
  32. CheckBook checkBookOb = new CheckBook();
  33.  
  34. public Check (String name, String date, double amount, CheckBook numChecks) { //normal checkler için
  35. numberCheck=checkBookOb.get_NumChecks();
  36. nameCheck=name;
  37. dateCheck=date;
  38. amountCheck=amount;
  39. }
  40. public Check ( String date, CheckBook numChecks) { //boş checkler için
  41. dateCheck=date;
  42. numberCheck=checkBookOb.get_NumChecks();
  43. }
  44.  
  45.  
  46. public void set_Name( String s ) { //check üzerindeki “name” değişkenine “s” değerini atar.
  47. nameCheck=s;
  48. }
  49. public String get_Name (){ //returns the name on the check.
  50. return nameCheck;
  51. }
  52. public double get_Amount (){ //returns the amount on the check.
  53. return amountCheck;
  54. }
  55. public String get_Date (){ //returns the date the check was written.
  56. return dateCheck;
  57. }
  58. public int get_Number(){
  59. return numberCheck;
  60. }
  61. }
  62.  
  63.  
©
CheckBook.java
  1.  
  2. /*************************************/
  3. /* */
  4. /* AHMET FARUK BİŞKİNLER */
  5. /* */
  6. /* CENG 102 JAVA PROGRAMMING */
  7. /* */
  8. /* LESSON */
  9. /* */
  10. /* COMPUTER ENGINEERING 07010441 */
  11. /* */
  12. /*************************************/
  13.  
  14.  
  15. //Yazılan çeklerin adet bilgisini numChecks integer değerini değiştirerek tutar
  16. public class CheckBook {
  17. private int numChecks=0;
  18.  
  19. public CheckBook(){
  20. numChecks++;
  21. }
  22.  
  23. public int get_NumChecks (){
  24. numChecks=numChecks+1;
  25. return numChecks;
  26. }
  27. }
  28.  
  29.  
  30.  
©
CheckTest.java
  1.  
  2.  
  3. import java.util.Scanner;
  4. public class CheckTest {
  5. static String name;
  6. static String date;
  7. static double amount;
  8.  
  9. public static void main(String args[]) {
  10.  
  11. Scanner input = new Scanner(System.in);
  12.  
  13. for ( int i=1 ; i != 5 ; i++ ){
  14.  
  15. System.out.print("Please Enter Your Name: ");
  16. name = input.next();
  17.  
  18. System.out.print("Please Enter The Date: ");
  19. date = input.next();
  20.  
  21. System.out.print("Please Enter Your Check's Amount: ");
  22. amount = input.nextDouble();
  23.  
  24. Check check1 = new Check(name, date, amount, null);
  25. CheckBook check2 = new CheckBook();
  26.  
  27. check1.set_Name(name);
  28.  
  29. System.out.print("\n\nThe Check Is Writen To: " + check1.get_Name());
  30. System.out.print("\nThe Check's Date Is: " + check1.get_Date());
  31. System.out.print("\nThe Check's Amount Is: " + check1.get_Amount());
  32. System.out.print("\nThe Check's Number Is: " + check2.get_NumChecks()+ "\n\n");
  33.  
  34. }
  35. }
  36. }
  37.  
  38.  
  39.  
©
Etiketler:
Son Güncelleme: Cuma, 08 Aralık 2006 21:32
 

Yorum ekle


Güvenlik kodu
Yenile