0% found this document useful (0 votes)
3 views9 pages

Java Source File

The document contains three Java programs: one for calculating commissions on property rentals and sales, another for determining the winner of a game based on player scores, and a third for converting numbers to words and removing duplicate characters from strings. Each program includes user input handling and methods for specific functionalities. The code demonstrates basic programming concepts such as loops, conditionals, and array manipulation.

Uploaded by

Angga Arta
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)
3 views9 pages

Java Source File

The document contains three Java programs: one for calculating commissions on property rentals and sales, another for determining the winner of a game based on player scores, and a third for converting numbers to words and removing duplicate characters from strings. Each program includes user input handling and methods for specific functionalities. The code demonstrates basic programming concepts such as loops, conditionals, and array manipulation.

Uploaded by

Angga Arta
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

Question 1

import [Link];

public class RentalOrSale {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

int numberofTransactions = 0;

double allCommissions = 0.0;

while (true) {

[Link]("Enter property <R>ental or <S>ale (<Q> to quit): ");

String option = [Link]().trim().toUpperCase();

if ([Link]("Q")) {

break;

if ([Link]("S")) {

[Link]("Enter selling price of property: $");

double price = [Link]();

[Link]();

double commission = price * 0.03;

if (commission < 1000.00) {

commission = 1000.00;

}
[Link]("Commission Earned: $" + commission);

numberofTransactions++;

allCommissions += commission;

else if ([Link]("R")) {

[Link]("Enter monthly gross rental of property: $");

double rent = [Link]();

[Link]("Number of years? ");

int years = [Link]();

[Link]();

double commission;

if (years <= 3) {

commission = rent * 1.25;

} else if (years <= 5) {

commission = rent * 1.5;

} else {

commission = rent * 1.75 + rent * 0.25 * (years - 5);

[Link]("Commission Earned: $" + commission);

numberofTransactions++;

allCommissions += commission;

else {
[Link]("Invalid input.");

if (numberofTransactions == 0) {

[Link]("No transaction for the day.");

} else {

[Link]("Number of transactions: " + numberofTransactions);

[Link]("Total commission earned $%.2f%n", allCommissions);

[Link]();

Question 3

import [Link];

public class PlayerScore {

// This the method to determine the winner by number of wins

public static String winnerByWins(int[] player1, int[] player2) {

int number1 = 0;

int number2 = 0;

for (int i = 0; i < [Link]; i++) {

if (player1[i] > player2[i]) {


number1++;

} else if (player2[i] > player1[i]) {

number2++;

if (number1 > number2) {

return "Player 1 with " + number1 + " wins";

} else {

return "Player 2 with " + number2 + " wins";

// This is the method to determine the winner by average score

public static String winnerByAverage(int[] player1, int[] player2) {

double total1 = 0;

double total2 = 0;

for (int i = 0; i < [Link]; i++) {

total1 += player1[i];

total2 += player2[i];

double avg1 = total1 / [Link];

double avg2 = total2 / [Link];

if (avg1 > avg2) {

return [Link]("Player 1 with average score of %.1f", avg1);


} else {

return [Link]("Player 2 with average score of %.1f", avg2);

// And this is the main method to drive the program

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

[Link]("How many sets (scores) per player? ");

int n = [Link]();

int[] player1 = new int[n];

int[] player2 = new int[n];

[Link]("Enter scores for Player 1:");

for (int i = 0; i < n; i++) {

[Link]("Set " + (i + 1) + ": ");

player1[i] = [Link]();

[Link]("Enter scores for Player 2:");

for (int i = 0; i < n; i++) {

[Link]("Set " + (i + 1) + ": ");

player2[i] = [Link]();

// To see the result


[Link]();

[Link]("Result by Wins: " + winnerByWins(player1, player2));

[Link]("Result by Average: " + winnerByAverage(player1, player2));

[Link]();

Question 2

import [Link];

public class DoubleRemover {

// Part (a) — Returns the word for a single digit

public static String digitStr(int digit) {

String[] words = {

"zero", "one", "two", "three", "four",

"five", "six", "seven", "eight", "nine"

};

return words[digit];

// Part (a) — Converts full number to digit-words

public static String numberInWord(int number) {

String str = [Link](number);

String result = "";

for (int i = 0; i < [Link](); i++) {


int digit = [Link]([Link](i));

result += digitStr(digit);

if (i != [Link]() - 1) {

result += " ";

return result;

// Part (b) — Removes matching doubles from start and end

public static String delDoubles(String input) {

int len = [Link]();

if (len < 2) {

return input;

if (len == 3 && [Link](0) == [Link](1) && [Link](1) == [Link](2))


{

return "";

if ([Link]() >= 2 && [Link](0) == [Link](1)) {

input = [Link](2);

if ([Link]() >= 2 && [Link]([Link]() - 2) == [Link]([Link]()


- 1)) {

input = [Link](0, [Link]() - 2);


}

return input;

// Main method for testing both parts

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

// --- Part (a): Number to Words ---

[Link]("Please enter a non-negative integer: ");

int number = [Link]();

if (number < 0) {

[Link]("You must enter a non-negative integer.");

} else {

String words = numberInWord(number);

[Link](words);

[Link]();

// --- Part (b): Remove Doubles ---

[Link]("Enter a string to test delDoubles: ");

[Link](); // consume leftover newline

String inputStr = [Link]();

String result = delDoubles(inputStr);


[Link]("After delDoubles: " + result);

[Link]();

You might also like