Exercise (Solutions)

E1

Create a program which will take three integers from scanner. The first integer can have values from 1 through 4. The second and third can be any numbers. If the first integer is 1, then the second and third integers should be added together. If the first integer is 2, then the second number should be divided by the third number. If the first integer is 3, then the second and third number should be multiplied. If the first integer is 4 then the second number should be subtracted by the third number. All results should be printed out.

E2

Write a program which takes a string from Scanner and counts how many ‘a’ alphabet is present in the string. For e.g., If the command line argument is “abundant” then the program should print 2. If the given string is “bold” then it is 0.

E3

Write a program called ArgumentSkipper that will print every other argument given on the command line. If the program was executed with the following in the command line:

java ArgumentSkipper one two three a b c d

the program would print

one
three
b
d

E4

The program below is supposed to calculate and show the time it takes for light to travel from the sun to the earth. It contains some logical errors. Fix the program so that it will show the correct value when run:

public class Sunlight {

    public static void main(String[] args){
        //Distance from sun ( 150 million kilometers
        int kmFromSun = 150000000;
        int lightSpeed = 299792458;// meters per second

        //Convert distance to meters:
        int mFromSun = kmFromSun * 1000;
        int seconds = mFromSun/ lightSpeed;
        System.out.println("seconds:" + seconds);

        System.out.print("Light will use ");
        printTime(seconds);
        System.out.println(" to travel from the sun to the earth.");
    }

    public static void printTime(int sec){
        int min = sec/60;
        sec = sec - (min * 60);
        System.out.println(min + " minute(s) and " + sec + " second(s)");
    }
}

E5

Write a program to print out all even numbers below 30.

E6

Create different versions of a program that finds all the primes below 100. Create one version that uses the naive approach and another version that uses Sieve of Eratosthenes. It is enough to print out just the number of primes below 100. However make sure you have coded in such a way that 100 can be replaced by 1000 or 9999 or any other number and your program still works.

E7

Create a program which takes a number as input from Scanner and prints out the complete Fact for that number from 1 to 10. The program should have a main method which calls generateFacts method which takes a number as input and outputs the facts as Array of integers.
The main method will iterate over the returned array and print it on to the console.

E8

Create a program which takes a string input and prints out the last three alphabets of the string.

E9

Write a console program to define and initialize a variable of type byte to 1 and then successively multiply it by 2 and display its value 8 times. Explain the reason for the last result.

E10

Write a guessing game. When the program starts, it picks a number in random between 20-50 and keeps it in memory and asks the user to guess a number. The user inputs a number between 20 and 50 both number inclusive. The system will print out the following hints to help the user win:

  • If the number the user input is below the number in memory by more than 10 then, print out "Your guess is too low. Try again."
  • If the number the user input is below the number in memory by equal to or less than 10 then, print out "Your guess is low. Try again."
  • If the number the user input is above the number in memory by more than 10 then, print out "Your guess is too high. Try again."
  • If the number the user input is above the number in memory by equal to or less than 10 then, print out "Your guess is high. Try again."

Once the user guesses the correct number, then print out "Congratulations, you guessed it right in " + the number of the times the user took to guess the number correctly.

Hint: To generate a random number between an upper and lower limit, you could use:

int randomNumber = (int) (Math.random() * (upperlimit - lowerlimit)) + lowerlimit;

javadoc for Math: https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html

results matching ""

    No results matching ""