SUKKUR IBA UNIVERSITY
OBJECT ORIENTED PROGRAMING
Assignment 1
B.E–II
Instructor: IRFAN LATIF MEMON
Marks: 10 STUDENT NAME
(Game: ATM machine)
Q[1]. (The Account class) Design a class named Account that contains:
A private int data field named id for the account (default 0).
A private double data field named balance for the account (default 0).
A private double data field named annualInterestRate that stores the current interest
rate (default 0). Assume all accounts have the same interest rate.
A private Date data field named dateCreated that stores the date when the account
was created.
A no-arg constructor that creates a default account.
A constructor that creates an account with the specified id and initial balance.
The accessor and mutator methods for id, balance, and annualInterestRate.
The accessor method for dateCreated.
A method named getMonthlyInterestRate() that returns the monthly interest rate.
A method named getMonthlyInterest() that returns the monthly interest.
A method named withdraw that withdraws a specified amount from the account.
A method named deposit that deposits a specified amount to the account.
Draw the UML diagram for the class and then implement the class.
(Hint: The method getMonthlyInterest() is to return monthly interest, not the interest
rate.
Monthly interest is balance * monthlyInterestRate. monthlyInterestRate is
annualInterestRate / 12. Note that annualInterestRate is a percentage, e.g., like
4.5%. You need to divide it by 100.)
Write a test program that creates an Account object with an account ID of 1122,
a balance of $20,000, and an annual interest rate of 4.5%. Use the withdraw method to
withdraw $2,500, use the deposit method to deposit $3,000, and print the balance, the
monthly interest, and the date when this account was created.
Use the Account class created to simulate an ATM machine
Create ten accounts in an array with id 0, 1, . . . , 9, and initial balance $100. The
system prompts the user to enter an id. If the id is entered incorrectly, ask the user
to enter a correct id. Once an id is accepted, the main menu is displayed as shown in
the sample run.
You can enter a choice 1 for viewing the current balance, 2 for withdrawing money, 3
for depositing money, and 4 for exiting the main menu. Once you exit, the system
will prompt for an id again. Thus, once the system starts, it will not stop.
(Display calendars) Write a program that prompts the user to enter the year and
first day of the year and displays the calendar table for the year on the console. For
example, if the user entered the year 2013, and 2 for Tuesday, January 1, 2013,
your program should display the calendar for each month in the year, as follows:
CODE:
package calender;
import java.util.Scanner;
public class CALENDER {
public static void main(String[] args) {
// Create Scanner
Scanner input = new Scanner(System.in);
System.out.println("WELCOME TO MY CALENDER");
System.out.println("SYEDA KIRAN ZAHID");
// Prompt the user to enter the year and first day of the year
System.out.print("Enter year: ");
int year = input.nextInt();
System.out.print("Enter first day of the year: ");
int day = input.nextInt();
// Create header of calender
String header;
System.out.println();
for (int month = 1; month <= 12; month++) {
header = "";
// Concat current month string to header
switch (month) {
case 1: header += "January "; break;
case 2: header += "February "; break;
case 3: header += "March "; break;
case 4: header += "April "; break;
case 5: header += "May "; break;
case 6: header += "June "; break;
case 7: header += "July "; break;
case 8: header += "August "; break;
case 9: header += "September "; break;
case 10: header += "October "; break;
case 11: header += "November "; break;
case 12: header += "December "; break;
// Concat current year to header
header += year + "";
// Center header string
for (int b = 0; b < 23 - (header.length() / 2); b++) {
System.out.print(" ");
// Display header and days of the week string
System.out.println(
header + "\n-----------------------------------------------\n " +
"Sun Mon Tue Wed Thu Fri Sat");
// Compute day of the week
day %= 7;
for (int b = 0; b <= day * 7; b++) {
System.out.print(" ");
// Compute last day of present month
int lastDay = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
lastDay += 31;
break;
case 4:
case 6:
case 9:
case 11:
lastDay += 30;
break;
default:
// Test for leap year
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
lastDay += 29;
else
lastDay += 28;
break;
}
// Display calender for current month
for (int d = 1; d <= lastDay; d++) {
// Add a black space before numbers less than 10
if (d < 10)
System.out.print(" ");
// Start new line after satuarday
if (day % 7 == 6)
System.out.print(d + "\n ");
else {
System.out.print(d + " ");
// After last day of the month go to new line
if (d == lastDay)
System.out.println();
day++; // increment day
System.out.println();
}
Q[2]. Case Study: Class GradeBook Using a Two- Dimensional
Array
In most semesters, students take several exams. Professors are likely to want to
analyze grades across the entire semester, both for a single student and for the class as
a whole.
Storing Student Grades in a Two-Dimensional Array in Class GradeBook
GradeBookclass that uses a two-dimensional array grades to store the grades of a number
of students on multiple exams.
Each row of the array represents a single student’s grades for the entire course, and
each column represents the grades of all the students who took a particular exam.
Class GradeBookTest passes the array as an argument to the GradeBook constructor. In this
example, we use a ten-by-three array for ten students’ grades on three exams.
Five methods perform array manipulations to process the grades. Each method is
similar to its counterpart in the earlier one-dimensional array version of GradeBook.
Method getMinimum determines the lowest grade of any student for the semester.
Method getMaximum determines the highest grade of any student for the semester.
Method getAverage determines a particular student’s semester average.
Method outputBarChart outputs a grade bar chart for the entire semester’s student grades.
Method outputGrades outputs the array in a tabular format, along with each student’s
semester average.
Method getsort use to sort the student in ascending
Method search use to search particular student information..
Code:
class GradeBook {
private String courseName; // name of course this grade book represents
private final int grades[][]; // two-dimensional array of student grades
// two-argument constructor initializes courseName and grades array
public GradeBook(String name, int gradesArray[][]) {
courseName = name; // initialize courseName
grades = gradesArray; // store grades
} // end two-argument GradeBook constructor
// method to set the course name
public void setCourseName(String name) {
courseName = name; // store the course name
} // end method setCourseName
// method to retrieve the course name
public String getCourseName() {
return courseName;
} // end method getCourseName
// display a welcome message to the GradeBook user
public void displayMessage() {
// getCourseName gets the name of the course
System.out.printf("Welcome to the grade book for%s!", getCourseName());
System.out.println("");
} // end method displayMessage
// perform various operations on the data
public void processGrades() {
// output grades array
outputGrades();
// call methods getMinimum and getMaximum
System.out.printf("%s %d%s %d",
"\nLowest grade in the grade book is", getMinimum(),
"\nHighest grade in the grade book is", getMaximum());
// output grade distribution chart of all grades on all tests
outputBarChart();
} // end method processGrades
// find minimum grade
public int getMinimum() {
// assume first element of grades array is smallest
int lowGrade = grades[0][0];
// loop through rows of grades array
for (int studentGrades[] : grades) {
// loop through columns of current row
for (int grade : studentGrades) {
// if grade less than lowGrade, assign it to lowGrade
if (grade < lowGrade) {
lowGrade = grade;
} // end inner for
} // end outer for
return lowGrade; // return lowest grade
} // end method getMinimum
// find maximum grade
public int getMaximum() {
// assume first element of grades array is largest
int highGrade = grades[0][0];
// loop through rows of grades array
for (int studentGrades[] : grades) {
// loop through columns of current row
for (int grade : studentGrades) {
// if grade greater than highGrade, assign it to highGrade
if (grade > highGrade) {
highGrade = grade;
} // end inner for
} // end outer for
return highGrade; // return highest grade
} // end method getMaximum
// determine average grade for particular set of grades
public double getAverage(int setOfGrades[]) {
int total = 0; // initialize total
// sum grades for one student
for (int grade : setOfGrades) {
total += grade;
}
// return average of grades
return (double) total / setOfGrades.length;
} // end method getAverage
// output bar chart displaying overall grade distribution
public void outputBarChart() {
System.out.println("\nOverall grade distribution:");
// stores frequency of grades in each range of 10 grades
int frequency[] = new int[11];
// for each grade in GradeBook, increment the appropriate frequency
for (int studentGrades[] : grades) {
for (int grade : studentGrades) {
++frequency[grade / 10];
} // end outer for
// for each grade frequency, print bar in chart
for (int count = 0; count < frequency.length; count++) {
// output bar label ( "00-09: ", ..., "90-99: ", "100: " )
if (count == 10) {
System.out.printf("%5d: ", 100);
} else {
System.out.printf("%02d-%02d: ",count * 10, count * 10 + 9);
// print bar of asterisks
for (int stars = 0; stars < frequency[count]; stars++) {
System.out.print("*");
}
System.out.println(); // start a new line of output
} // end outer for
} // end method outputBarChart
// output the contents of the grades array
public void outputGrades() {
System.out.println("The grades are:");
System.out.print(" "); // align column heads
System.out.println("");
// create a column heading for each of the tests
for (int test = 0; test < grades[0].length; test++) {
System.out.printf("Test %d ", test + 1);
System.out.println("Average"); // student average column heading
// create rows/columns of text representing array grades
for (int student = 0; student < grades.length; student++) {
System.out.printf("\nStudent %2d", student + 1);
for (int test : grades[student]) // output student's grades
System.out.printf("%8d", test);
// call method getAverage to calculate student's average grade;
// pass row of grades as the argument to getAverage
double average = getAverage(grades[student]);
System.out.printf("%9.2f", average);
} // end outer for
private String newline;
/**
* Get the value of newline
* @return the value of newline
*/
public String getNewline() {
return newline;
/**
* Set the value of newline
* @param newline new value of newline
*/
public void setNewline(String newline) {
this.newline = newline;
// end method outputGrades
} // end class GradeBook
public class GradeBookTest {
// main method begins program execution
public static void main(String args[]) {
// two-dimensional array of student grades
int gradesArray[][] = {{87, 96, 70},
{22, 34, 56},
{94, 100, 90},
{13, 76, 33},
{83, 65, 85},
{48, 87, 95},
{85, 75, 22},
{31, 94, 100},
{56, 72, 04},
{27, 93, 73}};
GradeBook myGradeBook = new GradeBook( " Introduction to Java Programming", gradesArray);
myGradeBook.displayMessage();
myGradeBook.processGrades();
} // end main
} // end class GradeBookTest
Q[3]. Cricket Match Design
(Cricket Match)To demonstrate the use of "loops" and "switch case", write a code of a crickte match.
Step 1: Do the toss between both teams and ask the toss winning team whether
they want to bat or bowl(use Switch Case).
Step 2: After toss the user should enter the number of overs.
Step 3: Use loop Structure to run the code over by over and make the code to seem like real time
match.
Step 4: Display the score board and results of the match.
Note: Use random function to take values like "Toss", "Runs", "extras" and "Wickets".
Sample Output