Skip to content

Instantly share code, notes, and snippets.

@dbc2201
Last active August 27, 2019 20:57
Show Gist options
  • Save dbc2201/6b5e4a456d239ab8807da2be2b268713 to your computer and use it in GitHub Desktop.
Save dbc2201/6b5e4a456d239ab8807da2be2b268713 to your computer and use it in GitHub Desktop.
Please complete the following programs in **`Java`** and confirm it with your respective faculty. You are free to use any IDE of your choice, writing the program's source code in a plain notepad program in also acceptable.

GLA University, 2019

BCSC0002: Object-Oriented Programming

Lab 03

Programming Exercises

Please complete the following programs in Java and confirm it with your respective faculty. You are free to use any IDE of your choice, writing the program's source code in a plain notepad program in also acceptable.

  1. Program 1

    Write a program in Java, to determine the sum of the following harmonic series for a given value of n;

    The value of n should be given interactively through the keyboard.

    Hint: Use the Scanner class from the package java.util.Scanner.

    Note: In Java programming language, we assume the whole numbers as the type int by default and the rational numbers as the type double by default.


  2. Program 2

    Write a program in Java, to read the price of an item in decimal form (like ₹45.95) and print the output in Rupees and Paise.

    Sample Input
    45.95
    
    Sample Output
    45 Rupees and 95 Paise
    

  3. Program 3

    Write a program in Java, to covert the given temperature in Fahrenheit to Celsius using the following conversion formula:

    and output the resulting value to the user.

    Sample Input
    97.4
    
    Sample Output
    36.333333
    

  4. Program 4

    Write a program in Java, to display the following to the console output window.

    1
    2	2
    3	3	3
    4	4	4	4
    5	5	5	5	5
    6	6	6	6	6	6
    7	7	7	7	7	7	7
    8	8	8	8	8	8	8	8
    9	9	9	9	9	9	9	9	9
    

  5. Program 5

    Write a program in Java, to display the day of the a date input by the user.

    Hint: You can study a sample formula for the given program here.

    Sample Input
    date: 15
    month: 8
    year: 2019
    
    Sample Output
    15th August 2019 was a Thursday!
    

Debugging Exercises

The following programs have some form of compile-time errors. Identify and remove the errors, so that the program runs successfully.

  1. Program 1

    public static void display() {
        int x = 123456;
        float f = 100.12;
        System.out.println("The float value = " + f);
    }
  2. Program 2

    public static void display() {
        int y;
        if (x > 10) {
            y = x;
        }
        System.out.println("The value of Y = " + y);
    }
  3. Program 3

    Modify the code in such a way, that the value of the variable pi is not modifiable (i.e it becomes a constant).

    public static void calculate() {
        float pi = 3.14f;
        System.out.println("The value of Pi = " + pi);
    }
  4. Program 4

    public static void convert() {
        int i = 1245;
        byte b = i;
        System.out.println("The value of the Byte variable b = " + b);
    }
  5. Program 5

    class ScopeDemo {
        public static void main(String[] args) {
            int m = 10;
            {
                int m = 20;
            }
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment