$20 Bonus + 25% OFF CLAIM OFFER

Place Your Order With Us Today And Go Stress-Free

Java Programming Basics
  • Download 30
  • Pages 5

DPIT111 Assignment 2

Objectives

  • Get familiar with relational and logical operators

  •  Get familiar with switch-case statement

  •  Get familiar with while loop and for loop


Ensure each file in your submission is runnable, compiling error in your file will result in 0 points for the current question. If it is runnable, though it fails to realize all functionalities, you can get partial points for what you have done.

Submission requirements:

  • Put each of Question 1, 2, 3 and 4 in a text file (.txt), rich text file (.rtf or .rtx) or java file (.java), and upload these 4 files one by one to the submission link for Assignment 2 on Moodle.

  • Submission via email is not acceptable without permission. 

  • Enquiries about the marks can only be made within a maximum of 1 week after the assignment results are published. After 1 week the marks cannot be changed.

  • Mark deductions: late submission, compilation errors, incorrect result, program is not up to spec (it does not follow the question’s instructions), poor comments, poor indentation, meaningless identifiers, required numeric constants are not defined, the program uses approaches which have not been covered in the lectures. The deductions here are merely a guide. Marks may also be deducted for other mistakes and poor practices.

Question 1

Write a program that reads in a character entered by the user and prints out a message to say whether the character is uppercase or lowercase based on the character’s ASCII code. Uppercase letters range from 65 to 90 in ACSII value, while lowercase letters range from 97 to 122.

Requirements

  • Your code must use if-else statements.

  • You must use the scanner to read in the user input.

  • The user input is always correct (input verification is not required).

  • You can compare a character to a number to check its ASCII value.

  • Your code must work exactly like the following example (the text in bold indicates the user input).


Example of the program output:

Example 1:
Enter a character: F
The character is uppercase.

Example 2:
Enter an integer: k
The character is lowercase.

Example 3:
Enter an integer: %
The character is not letter.

Answer

import java.util.Scanner;

public class CharCaseChecker {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);

        if(ch >= 'A' && ch <= 'Z') {
            System.out.println("The character is uppercase.");
        } else if(ch >= 'a' && ch <= 'z') {
            System.out.println("The character is lowercase.");
        } else {
            System.out.println("The character is not a letter.");
        }

        scanner.close();
    }
}

Also Read - Programming Assignment Help

Question 2 :

Assume there is an enum type variable declared as follows:

enum Colour {BLUE, BLUEGREEN, EQUALMIX, GREENBLUE, GREEN};

Write a program to ask the user to input two integers between 0 and 10 representing the amount of colour of blue and green that are mixed together. Your program will print out the colour based on the following rules:

  • If blue makes up twice the amount of green or more, then print BLUE, otherwise if there more blue than green, print BLUEGREEN.

  • If green makes up twice the amount of blue or more, then print GREEN, otherwise if there more green than blue, print GREENBLUE.

  • If there is an equal mix of both colours, print EQUALMIX.

REQUIREMENTS

  • Your code must use if-else statements.

  • Your code must use the enum to print the result of the colour mix.

  • The user input is always correct (input verification is not required).

  • Your code must work exactly like the following example (the text in bold indicates the user input).

Example of the program output:

Example 1:

Enter how much blue is in the mix (0-10): 8
Enter how much green is in the mix (0-10): 3

The resulting mix is BLUE.

Example 2:
Enter how much blue is in the mix (0-10): 7
Enter how much green is in the mix (0-10): 5

The resulting mix is BLUEGREEN.

Example 3:
Enter how much blue is in the mix (0-10): 4
Enter how much green is in the mix (0-10): 4

The resulting mix is EQUALMIX.

Example 4:
Enter how much blue is in the mix (0-10): 6
Enter how much green is in the mix (0-10): 9

The resulting mix is GREENBLUE.

Example 5:
Enter how much blue is in the mix (0-10): 2
Enter how much green is in the mix (0-10): 7

The resulting mix is GREEN.

Answer

import java.util.Scanner;

enum Colour {BLUE, BLUEGREEN, EQUALMIX, GREENBLUE, GREEN};

public class ColourMixer {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter how much blue is in the mix (0-10): ");
        int blue = scanner.nextInt();
        System.out.print("Enter how much green is in the mix (0-10): ");
        int green = scanner.nextInt();

        Colour result = null;
        if (blue >= green * 2) {
            result = Colour.BLUE;
        } else if (green >= blue * 2) {
            result = Colour.GREEN;
        } else if (blue > green) {
            result = Colour.BLUEGREEN;
        } else if (green > blue) {
            result = Colour.GREENBLUE;
        } else {
            result = Colour.EQUALMIX;
        }

        System.out.println("The resulting mix is " + result + ".");
        scanner.close();
    }
}

Also Read - Java Assignment Help

Question 3

Write a program to generate a random integer between 0 and 50 and then allow a user to choose whether they want to check if it is odd or even, double the number, or find the number’s square.

Requirements

  • Your code must use a switch-case statement to determine the menu option.

  • You can use the modulus (%) to determine if a number is odd or even.

  • The user input is always correct (input verification is not required).

  • Your code must work exactly like the following example (the text in bold indicates the user input).

Example of the program output (the text in bold indicates the user input):

Example 1:

Your random number is 32. What do you want to do with this number?
1) Determine if it is odd or even
2) Double this number
3) Find the square of this number
Please choose an option: 1
This number is even

Example 2:

Your random number is 17. What do you want to do with this number?
1) Determine if it is odd or even
2) Double this number
3) Find the square of this number
Please choose an option: 1
This number is odd

Example 3:

Your random number is 11. What do you want to do with this number?
1) Determine if it is odd or even
2) Double this number
3) Find the square of this number
Please choose an option: 2
The double of this number is 22

Example 4:
Your random number is 19. What do you want to do with this number?
1) Determine if it is odd or even
2) Double this number
3) Find the square of this number
Please choose an option: 3
The square of this number is 361

Answer

import java.util.Random;
import java.util.Scanner;

public class RandomNumber {
    public static void main(String[] args) {
        // Create a Scanner object to read user input
        Scanner scanner = new Scanner(System.in);

        // Generate a random number between 0 and 50
        Random random = new Random();
        int randomNumber = random.nextInt(51);

        // Display the random number to the user
        System.out.println("Your random number is " + randomNumber + ". What do you want to do with this number?");
        System.out.println("1) Determine if it is odd or even");
        System.out.println("2) Double this number");
        System.out.println("3) Find the square of this number");

        // Read the user's choice
        int choice = scanner.nextInt();

        // Use a switch-case statement to perform the user's chosen operation
        switch (choice) {
            case 1:
                if (randomNumber % 2 == 0) {
                    System.out.println("This number is even");
                } else {
                    System.out.println("This number is odd");
                }
                break;
            case 2:
                int doubleNumber = randomNumber * 2;
                System.out.println("The double of this number is " + doubleNumber);
                break;
            case 3:
                int squareNumber = randomNumber * randomNumber;
                System.out.println("The square of this number is " + squareNumber);
                break;
            default:
                System.out.println("Invalid choice");
                break;
        }

        // Close the scanner object
        scanner.close();
    }
}

Also Read - Computer Science Assignment Help

Question 4

Write a program to compute the cost of a plane ticket according to the following UML diagram and expressions. The base cost of the ticket is obtained from the user.

flightticketjava

Requirements

  • The method calcTicketPrice(double) is to calculate the total cost of the plane ticket based on its argument represented as the baseCost and return that cost back to the “main” method. The cost of the plane ticket is the baseCost plus the BOOKINGFEE, and then apply a TAX fee by adding that total multiplied by the tax rate.

  • The main() method should ask the user to input a double representing the baseCost and accordingly call the method calcTicketPrice, and then print the final ticket price based on the value returned from calcTicketPrice.

  • You must print the final ticket price with 2 decimal places.

  • Your code must work exactly as the specification and the output should be exactly the same as the output example.

Two examples of the program output (the texts in bold indicate the user input):

Example 1:
Enter the plane ticket base cost: 120
Full ticket price: $187.00

Example 2:
Enter the plane ticket base cost: 225.50
Full ticket price: $303.05

Answer

import java.util.Scanner;

public class FlightTicket {
    final static int BOOKINGFEE = 50; // read only
    final static double TAX = 0.1; // read only

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the plane ticket base cost: ");
        double baseCost = input.nextDouble();

        double fullPrice = calcTicketPrice(baseCost);
        System.out.printf("Full ticket price: $%.2f\n", fullPrice);
    }

    public static double calcTicketPrice(double baseCost) {
        double totalCost = baseCost + BOOKINGFEE;
        totalCost += totalCost * TAX;
        return totalCost;
    }
}

Also Read - Human-Centred Systems Design

We Can Help!

Are you confident that you will achieve the grade? Our best Expert will help you improve your grade

Order Now
Chat on WhatsApp
Chat
Call Now
Chat on WhatsApp
Call Now


Best Universities In Australia

Best In Countries

Upload your requirements and see your grades improving.

10K+ Satisfied Students. Order Now

Disclaimer: The reference papers given by DigiAssignmentHelp.com serve as model papers for students and are not to be presented as it is. These papers are intended to be used for reference & research purposes only.
Copyright © 2022 DigiAssignmentHelp.com. All rights reserved.

100% Secure Payment

paypal