100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
code java R127,00
Add to cart

Other

code java

 8 views  0 purchase

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

Preview 2 out of 8  pages

  • September 11, 2023
  • 8
  • 2023/2024
  • Other
  • Unknown
All documents for this subject (12)
avatar-seller
Tyrisc
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

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through EFT, credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying this summary from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller Tyrisc. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy this summary for R127,00. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

52510 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy summaries for 14 years now

Start selling
R127,00
  • (0)
Add to cart
Added