100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

code java

Rating
-
Sold
-
Pages
8
Uploaded on
11-09-2023
Written in
2023/2024

Past question papers on java code to practice on if you need to understand more java coding










Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
September 11, 2023
Number of pages
8
Written in
2023/2024
Type
Other
Person
Unknown

Subjects

Content preview

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
R127,00
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
Tyrisc

Get to know the seller

Seller avatar
Tyrisc University of KwaZulu Natal
View profile
Follow You need to be logged in order to follow users or courses
Sold
3
Member since
3 year
Number of followers
1
Documents
11
Last sold
3 year ago

0,0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions