Lab 10
Lab 10
Q1.
a) Implement a class that will represent a football team in a package called football.
package football;
}}
b) Add fields for the following properties, which cannot be accessed outside of the class.
name of the team , number of wins , number of losses
private String n;
private int w;
private int l;
c) Write a constructor that accepts the name of the team, the number of wins, and the
number of losses as arguments and sets the class properties to those values. This
constructor should be accessible from any package.
d) Write a second constructor that takes only the name of the team as an argument. This
constructor should set the name of the team to the argument and the set the number of
wins and losses to 0. (Hint: The body of this constructor should be one line and it should
call the first constructor.) This constructor should be accessible from any package.
e) Write methods that return the name of the team, the number of the wins, and the number
of losses. These methods should be accessible from any package.
h) Write a method that returns true when a team has a “good record,” meaning the team has
more wins than losses.
i) Finally, add a main method to the FootballTeam class. In the main method,
construct a FootballTeam named "Dubai" with 3 wins and 5 losses. Call the method
that returns true when the team has a good record and print out the result. Now make
CSIT111- Lab 9
three calls to the method that increases the number of wins by 1. Lastly, call the "good
record" method again and print out the result.
package FotBallTeam;
FootBall(String teamN){
this(teamN, 0, 0);
}
String getTeam_name(){
return this.teamN;
}
int getTeam_wins(){
return this.teamW;
}
int getTeam_losses(){
return this.teamL;
}
void plusTeam_wins(){
this.teamW += 1;
}
void plusTeam_losses(){
this.teamL += 1;
}
Boolean GRecord(){
return teamW > teamL;
}
CSIT111- Lab 9
for (int i=0; i<3; i++) {
team2.plusTeam_wins();
}
team2.plusTeam_losses();
team2.plusTeam_losses();
System.out.println(team2.GRecord());
Q2.
a) Open a text file and write the below records into the file.
Name ID Balance
Zainul 1234567 97.67
Zoubi 9786789 278.40
Shawa 8912345 356.78
Ussama 9876522 167.00
Islam 1123111 298.8
Abeer 9876766 300.0
Maryam 987656 9 299.0
c) Write a function that accepts all the accounts and also the account_ID. The function should
return the bankaccount whose id is account_ID. If does not find it should return a dummy account ( in
CSIT121 you will learn how to throw an exception and catch it). The dummy account should be
created using a default constructor
}}
Q4. How to display the 2D array
public static void main(String[] args) {
int[][] twod = {
CSIT111- Lab 9
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for (int i = 0; i < twod.length; i++)
{
for (int j = 0; j < twod[i].length; j++)
{
System.out.print(twod[i][j] + " ");
}
System.out.println();
}
}}//Main
}
return res;
}
public static ArrayList< Accounts> Bonus (ArrayList<Accounts>list){
int max=5;
int min=3;
for (Accounts ac:list) {
double balance =ac.getBalance();
double random= (int) Math.floor(Math.random()* (max-min+1)+min);
random=random/100;
balance=balance * (1+random);
ac.setBalance(balance);
}
return list;
CSIT111- Lab 9
File myfile=new File ("Accounts");
Scanner inf=new Scanner (myfile);
String str="";
ArrayList<Accounts> listAccounts=new ArrayList<>();
while (inf.hasNext()){
}
System.out.println(findAccounID(accountlist,1123111));
System.out.println(Bonus(accountlist));
}}
CSIT111- Lab 9