0% found this document useful (0 votes)
46 views

Assignment #1

The document contains 3 questions asking to write Java classes for different patterns and objects: 1. A Pattern class to print alphabet patterns in multiple rows with increasing columns. 2. A Car class with attributes like name, direction, position and methods to initialize, turn direction, and move position. 3. A Bank class with depositor name, account number, type and balance as attributes, and methods to assign values, deposit, withdraw and display details.

Uploaded by

pir zada
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Assignment #1

The document contains 3 questions asking to write Java classes for different patterns and objects: 1. A Pattern class to print alphabet patterns in multiple rows with increasing columns. 2. A Car class with attributes like name, direction, position and methods to initialize, turn direction, and move position. 3. A Bank class with depositor name, account number, type and balance as attributes, and methods to assign values, deposit, withdraw and display details.

Uploaded by

pir zada
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

/*

Q1: Write a class “shape” to generate a following pattern using


two methods.
A
B C
D E F
G H I J
K L M N
O P Q R S T U */
(a)
class Pattern
{
public static void main(String args[])
{
int alphabet = 65;//ASCII value of 'A'
for(int i = 1; i <6; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print((char)alphabet);
alphabet++;
}
System.out.println();
}
}
}
A
BC
DEF
GHIJ
KLMN
OPQRSTU
(b)
//Diamond Pattern
//Java is my favorite subject
Class DiamondShape
{
public static void main(String[] args)
{
int size = 7,odd = 1, nos = size/2; // nos =number of spaces
for (int i = 1; i <= size; i++) { // for number of rows i.e n rows
for (int k = nos; k >= 1; k--)// for number of spaces i.e
// 3,2,1,0,1,2,3 and so on
{
System.out.print(" ");
}
for (int j = 1; j <= odd; j++) { // for number of columns i.e
// 1,3,5,7,5,3,1
System.out.print("*");
}
System.out.println();
if (i < size/2+1) {
odd += 2; // columns increasing till center row
nos -= 1; // spaces decreasing till center row
} else {
odd -= 2; // columns decreasing
nos += 1; // spaces increasing

} *
} ***
} *****
} *******
*****
***
*
2. Write a class that contain the following attributes:
 The name of a car
 The direction of a car(E,W,N,S)
 The position of a car(from imaginary zero point)
 The class has the following member functions:
Initialize to attributes.
 Turn function to change the direction of a car to one step right
side(e.g if the direction of a car is E, it should be changed to S and
so on)
 Overload the function to change the direction to any side directly.
It should accept the direction as parameters.
 Move function to change the direction of car away from zero point.
It should have the direction as parameters.
class car
{
char direction;
int position;
public
car(char d,int p)
{
strcpy(direction,d);
position=p;
}
void turn()
{
switch(direction)
{
case 'N':
direction='E';
break;
case 'E':
direction='S';
break;
case 'S':
direction='W';
break;
case 'W':
direction='N';
break;
default:
System.out.println("ERROR");
break;
}
}
void turn(char new_direction)
{
strcpy (direction,new_direction);
}
void move(int position_change)
{
position=position+position_change;
}
};
void ()
{
car c('E',0);
System.out.println (" Current Direction : ");
System.out.print"\n Current Position : ");
c.turn();
c.move(25);
System.out.println" Current Direction : ");
System.out.println"\n Current Position : ");
c.turn('N');
c.move(25);
System.out.println("Current Direction : ");
System.out.println(" Current Position : ");
}
/*
Q3. Write a class for a bank account that includes the following data
members.
 Name of the depositor
 Account Number
 Type of account
 Balance amount in the account
1. The class also contain the following member functions:
 Assign initial value
 Deposit function to deposit some value.
 It should accept the amount as parameters.
 Withdraw function to withdraw an amount after checking the
balance.
 It should accept the amount as parameter.
 Display function to display name and balance
*/

public class bank


{
public int acc_no;
public int bal;
public String type = new String(new char[30]);
public String name = new String(new char[30]);
public final void assign()
{
name = "PirZada khan";
acc_no = 144;
type = "saving";
bal = 1000;
}
public final void deposit()
{
int amt;
System.out.print("enter the amount which you want to deposit:");
amt = Integer.parseInt());
bal = bal + amt;
}
public final void withdraw()
{
System.out.print("enter how much money you want to withdraw");
amt=Integer.parseInt( ));
}
public final void display()
{
System.out.print("\nname:-");
System.out.print(name);
System.out.print("account number:-");
System.out.print(acc_no);
System.out.print("type of account:-");
System.out.print(type);
System.out.print("balance amount:-");
System.out.print(bal);
}
}
 

Technology has made it easier for students to learn with


devices new, but nothing can come close to the experience of
being taught by an inspirational teacher like you.
Thank you

You might also like