Practice Questions
Note:
These are practical programming questions taken from previous assessments and tutorials. They cover
the entire syllabus.
Write a class that represents the outcome of a soccer match. Its member attributes should include:
• The names of each team.
• The scores of each team.
Its member methods should include:
• A constructor that allows the programmer to set the names and scores of each team.
• A constructor that allows the programmer to set the names of each team, but sets the score
of each team to 0.
• set() methods to set the score attributes.
• a Result() method that returns the name of the winning team, or "Draw" if the match is
drawn
The code below is used to get the user to enter an even integer - even integers leave no remainder
when divided by 2.
If the user enters a number that is not even, it will ask the user to enter a number again. However,
this code needs to be changed to prompt the user when they enter a non-integer.
a) Write the code that needs to be added to handle the case where the user enters a non-integer.
Also, give any existing lines of that need to be changed, by giving the line number and the new line of
code.
b) Where should the code from part a) be added? Give the line number.
Page 1 of 8
, The following class is used to give a simple representation of the income, expenses and profit of a
business. There are parts of the class that are missing that you need to write.
public class Business {
private float income, expenses, profit;
//###################################
// Your answer to Part (A) will go here.
//###################################
void setIncome(float newIncome) {
income = newIncome;
}
//###################################
// Your answer to Parts (B), (C) and (D) will go here.
//###################################
}
• (10 marks) (A) Write two constructors that initialise the values of the income and expenses
variables. One constructor should allow the user to specify the initial values of these
variables, the other constructor should initialise these variables to 0.
• (5 marks) (B) Write a method CalcProfit() that sets the value of the profit variable. Profit is
the income less then expenses.
• (5 marks) (C) Write a method PerProfit() that recalculates the profit using the method
written in part (B) and returns the profit as a percentage of the income.
• (5 marks) (D) Write a method isProfitable() that recalculates the profit using the method
written in part (B) and returns a true value if the profit is positive and a false value if the
profit is negative.
• (2 marks) (E) Rewrite the variable declarations to make only the income variable accessible
outside the class.
The following class is used to represent a flour mill. The mill uses wheat to produce flour. The boolean
Error attribute is used to indicate that there was not enough wheat to produce the required number
of bags of flour.
public class FlourMill {
private int FlourBags = 0;
private float WheatKgs = 0;
boolean Error = false;
FlourMill(float InitialWheat) {
WheatKgs = InitialWheat;
Page 2 of 8
Note:
These are practical programming questions taken from previous assessments and tutorials. They cover
the entire syllabus.
Write a class that represents the outcome of a soccer match. Its member attributes should include:
• The names of each team.
• The scores of each team.
Its member methods should include:
• A constructor that allows the programmer to set the names and scores of each team.
• A constructor that allows the programmer to set the names of each team, but sets the score
of each team to 0.
• set() methods to set the score attributes.
• a Result() method that returns the name of the winning team, or "Draw" if the match is
drawn
The code below is used to get the user to enter an even integer - even integers leave no remainder
when divided by 2.
If the user enters a number that is not even, it will ask the user to enter a number again. However,
this code needs to be changed to prompt the user when they enter a non-integer.
a) Write the code that needs to be added to handle the case where the user enters a non-integer.
Also, give any existing lines of that need to be changed, by giving the line number and the new line of
code.
b) Where should the code from part a) be added? Give the line number.
Page 1 of 8
, The following class is used to give a simple representation of the income, expenses and profit of a
business. There are parts of the class that are missing that you need to write.
public class Business {
private float income, expenses, profit;
//###################################
// Your answer to Part (A) will go here.
//###################################
void setIncome(float newIncome) {
income = newIncome;
}
//###################################
// Your answer to Parts (B), (C) and (D) will go here.
//###################################
}
• (10 marks) (A) Write two constructors that initialise the values of the income and expenses
variables. One constructor should allow the user to specify the initial values of these
variables, the other constructor should initialise these variables to 0.
• (5 marks) (B) Write a method CalcProfit() that sets the value of the profit variable. Profit is
the income less then expenses.
• (5 marks) (C) Write a method PerProfit() that recalculates the profit using the method
written in part (B) and returns the profit as a percentage of the income.
• (5 marks) (D) Write a method isProfitable() that recalculates the profit using the method
written in part (B) and returns a true value if the profit is positive and a false value if the
profit is negative.
• (2 marks) (E) Rewrite the variable declarations to make only the income variable accessible
outside the class.
The following class is used to represent a flour mill. The mill uses wheat to produce flour. The boolean
Error attribute is used to indicate that there was not enough wheat to produce the required number
of bags of flour.
public class FlourMill {
private int FlourBags = 0;
private float WheatKgs = 0;
boolean Error = false;
FlourMill(float InitialWheat) {
WheatKgs = InitialWheat;
Page 2 of 8