0% found this document useful (0 votes)
57 views5 pages

Lab 10

The document provides instructions for several programming assignments: 1. Create a FootballTeam class with fields for team name, wins, losses and methods to retrieve/update these values and check if the team has a winning record. 2. Write code to read bank account data from a text file into an Accounts data structure, find an account by ID, add a random bonus to all accounts. 3. Demonstrate how to create and access 2D arrays of different data types and extract columns/rows. Flatten arrays into 1D static and dynamic arrays. 4. Experiment with String and ArrayList methods like split, contains, add, remove etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views5 pages

Lab 10

The document provides instructions for several programming assignments: 1. Create a FootballTeam class with fields for team name, wins, losses and methods to retrieve/update these values and check if the team has a winning record. 2. Write code to read bank account data from a text file into an Accounts data structure, find an account by ID, add a random bonus to all accounts. 3. Demonstrate how to create and access 2D arrays of different data types and extract columns/rows. Flatten arrays into 1D static and dynamic arrays. 4. Experiment with String and ArrayList methods like split, contains, add, remove etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Faculty of Engineering and Information Sciences          

CSIT111    Programming Fundamentals                                Lab 9-10 Due week 10.

Q1.

a) Implement a class that will represent a football team in a package called football.

package football;

public class football_team {

public static void main(String[] args){

}}

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.

f) Write a method to increase the numbers of wins by 1

g) Another method to increase the number of losses by one.

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.

j) Similarly construct a FootballTeam named "AbuDhabi" using one parameter


constructor. Call the method that returns true when the team has a good record and print
out the result. Now make three calls to the method that increases the number of wins by 1.
and 2 calls that decreases the number of wins by 1. Lastly, call the "good record" method
again and print out the result.

package FotBallTeam;

public class FootBall {


private String teamN;
private int teamW;
private int teamL;

FootBall(String teamN, int teamW, int teamL){


this.teamN = teamN;
this.teamW = teamW;
this.teamL = teamL;
}

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;
}

public static void main(String[] args) {


FootBall team = new FootBall("Dxb",3,5);
System.out.println(team.GRecord());
for (int i=0; i<3; i++) {
team.plusTeam_wins();
}
System.out.println(team.GRecord());
FootBall team2 = new FootBall("AbuDhabi");
System.out.println(team2.GRecord());

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

b) Read the records into a data structure of Accounts

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

d) Write a function that adds a random bonus from 3% to 5% to all accounts

e) Display the banks accounts using enhance for.

Q3. How to create a 2D static array of doubles, Strings,


package lab9_part2;

public class part2 {

public static void main(String[] args) {


// TODO Auto-generated method stub
String[][] str = { {"Mr. ", "Mrs. ", "Ms. "}, {"Klaus"} };
System.out.println(str[0][0] + str[1][0]);
System.out.println(str[0][1] + str[1][0]);

}}
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

Q5. How to extract a given column (write a function)

Q6. How to extract a given arrow (write a function)


Q7. How to flatten the above array into a static array
Q8. How to flatten the above into a dynamic list
Q9. Note we did not teach how to created a multidimensional dynamic array (we will teach in
CSIT121)
Q10. Experiment with methods (services) offered by the String method (split, charAt, valueof, equals,
contains, substring, indexof, startswith
Q11. Experiment with services related to dynamics : initialize from a static array, add, get, remove,
index, size, subList, contains,

public class lab_10 {


public static Accounts findAccountID(ArrayList<Accounts>list, int accID) {
Accounts res= new Accounts();
for (Accounts ac: list) {
if (ac.getID()==accID) return ac;

}
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;

public static void main(String[] args) throws Exception {

CSIT111- Lab 9
File myfile=new File ("Accounts");
Scanner inf=new Scanner (myfile);
String str="";
ArrayList<Accounts> listAccounts=new ArrayList<>();
while (inf.hasNext()){

Accounts ac=(new Accounts (inf.next(), inf.nextInt(), inf.nextDouble());


accountslist.add(ac);

}
System.out.println(findAccounID(accountlist,1123111));
System.out.println(Bonus(accountlist));

}}

CSIT111- Lab 9

You might also like