CSIT111/811 Lab Exercises
Lab week5
Objectives
- Java code analysis and debugging
- Data types, arithmetic expressions, Math methods
- Data type conversion
- Strings
Exercise 1:Analyse, compile and fix bugs in the following program
{`public class CircleBugs {
public static void main(String[] args) {
double t = Double.parseDouble(args[0]);
int r = Integer.parseInt(args[0]);
System.out.println("r = " + r + ", t = " + t)
double c = 2 * Math.PI * r;
double A = Math.PI * r * r;
double x = r * cos(t);
double y = r * sin(t);
System.out.println("c = " + c );
System.out.println("A = " + A );
System.out.println("x = " + x + ", " + "y = " + y );
}
} `}
After all errors are corrected, you would have the output as follows:
java CircleBugs 1.2 0.6 r = 1.2, t = 0.6 c = 7.5398223686155035 a = 4.523893421169302 x = 0.9904027378916139, y = 0.6775709680740424
Exercise 2: If you carefully look at the ASCII Character Table, you'll notice that the difference between uppercase and lowercase character codes is 32. Implement a program that
- Prompts the user to enter a lowercase character
- Reads the entered character
- Converts this lowercase character to the corresponding uppercase character - Displays the uppercase character
Exercise 3: Implement a program that:
- Prompts the user to enter a floating point number nA
- Calculates nB = nA5
- Displays nB with 1 decimal point precision if it is less than 199.0, otherwise it displays a constant value 199.0
When you implement this solution you can use only material covered in Lectures 1-4.
Exercise 4: Implement a program that:
- Reads a floating point number nA from the command line
- Calculates nB = ena
- Coverts nb to an integer number discarding its decimal part
- Displays nB
When you implement this solution you can use only material covered in Lectures 1-4.
Exercise 5: Implement this simple program:
{` import java.util.Scanner;
class InpTest
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a double: ");
double db = keyboard.nextDouble();
System.out.println("Input was: " + db);
System.out.print("Enter an integer: ");
int ia = keyboard.nextInt();
System.out.println("Input was: " + ia);
}
} `}
The program works properly if your input is right. However, try to enter two (or more) double numbers before you press Enter.
- Explain why the program is not working properly with this input
- Try to implement a simple solution (doesn't need to be perfect) that proves your explanation