Escolar Documentos
Profissional Documentos
Cultura Documentos
case
Exercise 3: Write a program to reverse a 6 digit number using while or for loop.
Exercise 4: Write a program to calculate your age as of today by giving your date of birth as
input. Example you are born on 11 april 1989 and today is 22 april 2019 then output will be
Exercise 5: Write a Java program to get the current time in New York.
Exercise 7: Write a Java method to display the current date and time.
Exercise 9: Write a Java program to find the longest word in a text file.
Exercise 10: Write a Java program that accepts two double variables and test if both strictly
between 0 and 1 and false otherwise. If input is not a double but a integer user is prompted to
input double variable only.
Exercise 11: Write a program to find the GCD - greatest common divisor of two number using
for loop.
Exercise 12 : Write a program to print the Fibonacci series based on user input. Fibonacci series
of first 6 numbers is 0,1,1,2,3,5
Exercise 13: Write a program to swap two integers without using a temporary variable.
Exercise 15: Write a program to throw a user defined exception. eg Create a exception which is
thrown when the input string is less than 100.
Exercise 16:Given an array of ints, we'll say that a triple is a value appearing 3 times in a row in
the array. Return true if the array does not contain any triples.
Sample input/output
noTriples([1, 1, 2, 2, 1]) → true
noTriples([1, 1, 2, 2, 2, 1]) → false
noTriples([1, 1, 1, 2, 2, 2, 1]) → false
Exercise 17: Given 3 int values, a b c, return their sum. However, if one of the values is 13 then
it does not count towards the sum and values to its right do not count. So for example, if b is 13,
then both b and c do not count.
Sample input/output
luckySum(1, 2, 3) → 6
luckySum(1, 2, 13) → 3
luckySum(1, 13, 3) → 1
Exercise 18:Given an array of scores, return true if each score is equal or greater than the one
before. The array will be length 2 or more.
Sample input/output
scoresIncreasing([1, 3, 4]) → true
scoresIncreasing([1, 3, 2]) → false
scoresIncreasing([1, 1, 4]) → true
Exercise 19:
Return true if the given non-negative number is a multiple of 3 or 5, but not both. Use the %
"mod" operator
old35(3) → true
old35(10) → true
old35(15) → false
Exercise 20 : You have a red lottery ticket showing ints a, b, and c, each of which is 0, 1, or 2. If
they are all the value 2, the result is 10. Otherwise if they are all the same, the result is 5.
Otherwise so long as both b and c are different from a, the result is 1. Otherwise the result is 0.
Sample input/output
redTicket(2, 2, 2) → 10
redTicket(2, 2, 1) → 0
redTicket(0, 0, 0) → 5
Exercise 21 : Java Program to Calculate the Power of a Number using while loop only with using
ready made Math.pow() method. Eg base=3 exponent = 4 then result will be 3x3x3x3=81
Exercise 22 : Write a Java method to check whether a string is a valid password. Go to the editor
Password rules:
Sample input/Output
nearHundred(93) → true
nearHundred(90) → true
nearHundred(89) → false"
Exercise 25 : Write a program to check whether a number can be Expressed as Sum of Two
Prime Numbers .Sample input/output
Enter number : 34
34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17
Exercise 26: Create a class to print the area of a square and a rectangle. The class has two
methods with the same name but different number of parameters. The method for printing area of
rectangle has two parameters which are length and breadth respectively while the other method
for printing area of square has one parameter which is side of square.
Exercise 27 : Create a class 'Student' with three data members which are name, age and address.
The constructor of the class assigns default values name as "unknown", age as '0' and address as
"not available". It has two members with the same name 'setInfo'. First method has two
parameters for name and age and assigns the same whereas the second method takes has three
parameters which are assigned to name, age and address respectively. Print the name, age and
address of 10 students.
Hint - Use array of objects.
Exercise 28 : A boy has his money deposited $1000, $1500 and $2000 in banks-Bank A, Bank B
and Bank C respectively. We have to print the money deposited by him in a particular bank.
Create a class 'Bank' with a method 'getBalance' which returns 0. Make its three subclasses
named 'BankA', 'BankB' and 'BankC' with a method with the same name 'getBalance' which
returns the amount deposited in that particular bank. Call the method 'getBalance' by the object
of each of the three banks.
Exercise 29: Create a class to print an integer and a character with two methods having the same
name but different sequence of the integer and the character parameters.
For example, if the parameters of the first method are of the form (int n, char c), then that of the
second method will be of the form (char c, int n).
Exercise 30 : Create a class 'Degree' having a method 'getDegree' that prints "I got a degree". It
has two subclasses namely 'Undergraduate' and 'Postgraduate' each having a method with the
same name that prints "I am an Undergraduate" and "I am a Postgraduate" respectively. Call the
method by creating an object of each of the three classes.