Java-practice-exercises
Java-practice-exercises
System.out.println(“Hello, World!”);
a) a method:
b) a class:
c) an argument:
a) System.out.println(“Hello, World!”)
b) System.out.print(Hello World);
c) system.out.println(“Hello, World!”);
Page 1 of 2
8. a) What is the purpose of program comments or “internal program documentation”?
b) What are the two basic ways to create program comments in Java?
9. a) What is the complete path and file name of the Java compiler?
b) What is the complete path and file name of the Java interpreter?
Write a Java application that displays the following information about yourself on the console(i.e. not
using dialogs)
Your first and last name
Your previous programming experience (how long? what language(s)?)
Your favourite subject in high school (or university, or wherever you were before this)
Your least favourite subject in high school (or university, or wherever you were before this)
A list of interests or things you enjoy doing in your spare time
Anything else interesting about yourself
Programs:
1. Create a program that displays a list of your three favourite foods, one food
per line. Example:
2. What is the minimum number of statements you could use in the main()
method to produce the output from question 1?
3. Reformat the following code segments by hand so that they follow proper
standards:
a)
public class BadCode { public static void main(String[]
args){ System.out.println("Flowers for Algernon");}}
b)
public class
BadCode { public static void main(String[]
args){ System.out.print("6/4*2 is ");System.
Page 2 of 2
out.println(6/4*2);}}
*
***
*****
*******
a. object
b. instance
c. attribute
d. state of an object
e. behaviour of an object
5. For each of the following things below, list some of the attributes or properties you might define if that
thing were an object in a program.
a. Invoice
b. TVShow
c. HockeyTeam
Page 3 of 2
1. Find the errors in each of the following code listings:
a. Listing a)
1 public class Qu2PartA
2 {
3 public static void main(String[] args)
4 {
5 width = 15
6 area = length * width;
7 System.out.println("The area is " + area);
}
8
}
9
b. Listing b)
1 public class Qu2PartB
2 {
3 public static void main(String[] args)
{
4
int length, width, area;
5 area = length * width;
6 length = 20;
7 width = 15;
8 System.out.println("The area is " + area);
9 }
10 }
c. Listing c)
1 public class Qu2PartC
2 {
public static void main(String[] args)
3
{
4 int length = 20, width = 15, area;
5 length * width = area;
6 System.out.println("The area is " + area);
7 }
8 }
Page 4 of 2
2. Write a program that calculates the amount of money to tip a wait person by coding the
following tasks:
7. Write the following expression in proper Java syntax. Use math methods where appropriate.
10. There are three errors in the code below. For each error, describe the error and indicate which of
the three error types it is (Execution, Compilation, or Logic)
Page 6 of 2
1. Examine the code listing below. What do you think the output should be?
Write this down. Then run the program and see if you're correct.
1
2 public class Question2
3 {
4 public static void main(String[] args)
{
5 int factor = 2;
6 int sum = 10;
7 System.out.println("sum is " + sum);
8 sum *= factor;
9 System.out.println("sum is now " + sum);
sum *= factor;
10 System.out.println("sum is now " + sum);
11 sum *= factor;
12 System.out.println("sum is now " + sum);
13 }
}
14
15
2. How do you think the output would change if you wrote the program in
question 1 like this:
1
2 public class Question3
3 {
4 public static void main(String[] args)
{
5 int factor = 2;
6 int sum = 10;
7 System.out.println("sum is " + sum);
8 sum *= factor;
9 sum *= factor;
sum *= factor;
10 System.out.println("sum is now " + sum);
11 System.out.println("sum is now " + sum);
12 System.out.println("sum is now " + sum);
13 }
}
14
15
Page 7 of 2
Logical Operations
1. Given that a = 5, b = 2, c = 4, and d = 5, what is the result of each of the
following Java expression?
a. a == 5
b. b * d == c * c
c. d % b * c > 5 || c % b * d < 7
d. d % b * c > 5 && c % b * d < 7
Page 8 of 2
3. For each of the following statements, assign variable names for the unknowns
and rewrite the statements as relational expressions.
1. A customer's age is 65 or more.
2. The temperature is less than 0 degrees.
3. A person's height is over 6 feet.
4. The current month is 12 (December).
5. The user enters the name "Fred".
6. The user enters the name "Fred" or "Wilma".
7. The person's age is 65 or more and their sub total is more than $100.
8. The current day is the 7th of the 4th month.
9. A person is older than 55 or has been at the company for more than 25 years.
10. A width of a wall is less than 4 metres but more than 3 metres.
11. An employee's department number is less than 500 but greater than 1, and
they've been at the company more than 25 years.
Page 9 of 2
1. Copy the code below into a main() method of a new program, and fill in the
missing code by following the instructions in the comments:
1
2 public class ScannerExercise {
3
4 public static void main(String[] args) {
5
6 // construct a scanner to get keyboard input
7
8
9 // ask the user for a decimal number
// (add the stmt to retrieve the value and store in an
10 // appropriate variable)
11 System.out.print("Enter a decimal number: ");
12
13
14 // calculate the number times itself (the square)
15 // and store in an appropriate variable (which needs
16 // to be declared - see last statement below where
// the variable is being used)
17
18
19 // user wants to see the result, this is finished so
20 // nothing to do here unless you used different variable name)
21 System.out.println("Your number squared is "
22 + square);
23 }
}
24
25
Page 10 of
2
1. Use the Math class documentation as a reference. What Math class methods
would you use to perform the following tasks:
Page 11 of
2
1. Write a program that finds the ASCII/Unicode code for each of the following
character values:
a. '7'
b. '1'
c. 'a'
d. 'A'
e. 'z'
f. 'Z'
g. '*'
2. Open the following chart in a new browser window/tab: Simple ASCII Table.
Exercises
1. How would you use casting to solve the problem in the TestData example in
the first section?
a. (int)dNum1
b. (int)dNum2
Page 12 of
2
c. (int)dNum3
d. (int)(dNum1 + dNum3)
e. (int)dNum1 + dNum3
f. (double)((int)dNum1) + dNum3
g. (double)((int)(dNum1 + dNum3))
Exercise
Copy the following program. Compile it, and run it. Test the program with each
of the input values below, and for each test, describe what happens and why.
1. 5
2. 5.0
3. five
Page 13 of
2
1. a) What is a multi-sided if-statement used for?
Page 14 of
2
3. b) Rewrite the code in 3.a) as an if/else-if.
a)
double area = 0;
if (length > 0);
{
area = length * 25.5;
System.out.printf(“Area: %.2f%n”, area);
}
b)
Scanner scan = new Scanner(System.in);
System.out.print(“Enter a whole number: “);
int num = scan.nextInt();
if (num % 2 = 0)
System.out.println(“Your number is even.”);
else
System.out.println(“Your number is odd.”);
Page 15 of
2
1) A. Write a loop that prints the numbers from 1 to 10 with their squares,
like this:
B. How would you make the loop do the same output backwards (from 10 to
1)?
Page 16 of
2
4) a. Write a program that requests a final grade. Use a do-loop to request the grade continuously as
long as the grade entered is invalid. A grade is invalid if it is less than 0 or greater than 100. After a
valid grade is entered, display it on the screen.
4) b. Modify the above program so that the user can cancel by entering the value 999.
4) c. Modify the program in 1. a. so that the user has only 5 tries to enter the grade. After 5 tries, the
program terminates.
5) a. For this program, use either a while-loop or a do-loop, whichever you think is most efficient.
Write a program that records scientific test results (they'll have decimal values) from the user. As each
test result is entered, add it to a total. After all the test results have been entered, display the total.
You don't know how many test results they'll enter, so after each result is entered, prompt the user
with a question such as "Would you like to enter another test result? (Y/N)". The user can answer
"Yes" or "No" too this question, or even just "Y" or "N". To capture this, we would use the Scanner's
next() method to grab only the first word of the user input:
However, we can make this program more efficient if we use a character value instead of a string, and
just compare the first letter of the user's input (Y or N). There is a method in the String class called
charAt(index). You give charAt() a string index (the position number, where the first character is
position 0, the second position 1, etc) of the character you'd like to have and it will return that
character at the specified index as a char value. For example:
So since we only what the first character that the user types, we can use:
char keepGoing = 'Y'; // initialize to yes
... // code code code
keepGoing = in.next().charAt(0);
Next, we want to see if the user says 'Y' to our prompt, meaning they'd like to enter another test
result. So our loop condition could be something like keepGoing == 'y' || keepGoing == 'Y' because we
don't know if the user will type an upper-case or a lower-case answer. If you prefer, you can use one of
the String class's methods toUpperCase() or toLowerCase() to convert the user's answer to upper- or
lower-case, and then compare. For example, I will use upper-case comparisons:
keepGoing = in.next().toUpperCase().charAt(0);
Page 17 of
2
1. Using loop statement write a program that prompts the user to enter 5 integer values:
i. Find and display the Largest and Smallest number
ii. Display whether the number is Even or Odd
iii. Display whether the number is negative, positive or zero
iv. Calculate the Sum and Average of the Even numbers
2. Write a program that randomly generates an integer between 0 and 100, inclusive.
The program prompts the user to enter a number continuously until the number matches the randomly
generated number. For each user input, the program tells the user whether the input is too low or too
high, so the user can choose the next input intelligently.
Page 18 of
2
PROG10082 - Object Oriented Programming 1
Exercise
Part A.
A.2 Write an if-else statement for y = |x – 1|. Assume that x and y are
already declared.
Part B. Show the output of the following code: (write the output next to
each println statement if the println statement is executed in the
program).
System.out.println((int)(Math.random()));
System.out.println(Math.pow(2, 3));
System.out.println(34 % 7);
int number = 4;
if (number % 3 == 0)
System.out.println(3 * number);
System.out.println(4 * number);
int x = 943;
System.out.println(x / 100);
System.out.println(x % 100);
int y = -1;
1
y++;
System.out.println(y);
}
}
Part C:
<Output>
Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 0
Enter the dollar amount: 100
$100.0 is 681.0 Yuan
<End Output>
<Output>
Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 1
Enter the RMB amount: 10000
10000.0 Yuan is $1468.43
<End Output>
<Output>
Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 5
Incorrect input
<End Output>
2
2. Write a program that prompts the user to enter an integer. If the
number is a multiple of 5, print HiFive. If the number is divisible by 2
or 3, print Georgia. Here are the sample runs:
<Output>
Enter an integer: 6
Georgia
<End Output>
<Output>
Enter an integer: 15
HiFive Georgia
<End Output>
<Output>
Enter an integer: 25
HiFive
<End Output>
<Output>
Enter an integer: 1
<End Output>
a. 76
b. 76.0252175
c. 76.03
d. 76.02
if (x > 0)
System.out.print("x is greater than 0");
else if (x < 0)
System.out.print("x is less than 0");
else
System.out.print("x equals 0");
a. x is less than 0
b. x is greater than 0
3
c. x equals 0
d. None
#
3. Analyze the following code:
Code 1:
boolean even;
if (number % 2 == 0)
even = true;
else
even = false;
Code 2:
#
4. What is x after evaluating
x = (2 > 3) ? 2 : 3;
a. 5
b. 2
c. 3
d. 4
#
5. Analyze the following code.
int x = 0;
if (x > 0);
{
System.out.println("x");
}
4
d. Nothing is printed because x > 0 is false.
#
6. To declare a constant MAX_LENGTH inside a method with value 99.98, you write
#
7. Which of the following is a constant, according to Java naming conventions?
a. read
b. MAX_VALUE
c. ReadInt
d. Test
8. Which of the following code displays the area of a circle if the radius is
positive.
5
Exercises
1. For each of the following, declare an array with an appropriate name and type
and allocate an appropriate amount of storage.
2. Fill in the elements for the values[] array (it has 10 elements) as the following
code executes:
int counter = 1;
values[0] = 10;
values[counter] = counter;
counter++;
values[5] = counter;
values[9] = values[5] + counter;
values[counter] = values[9] - values[1];
values[9] += ++counter;
Array: values[10]
3. Write the code to display the values[] array (from the previous exercise)
backwards.
4. a. Write a program that uses a char[] array to store the characters in a
sentence. Ask the user for a sentence, and then store that string into the char[]
array.
b. Modify the above program to do a search/replace for one letter in the char[]
array. Ask the user what letter they'd like to replace, and then the letter they'd
like to replace it with. After doing the search/replace, display the sentence.
This work is the intellectual property of Sheridan College. Any further copying and distribution outside of class
must be within the copyright law. Posting to commercial sites for profit is prohibited. © Wendi Jollymore, et al
Exercises
1. Record 10 integer values from the user and store them in an array. After
recording the 10 values, calculate and display:
• The average
• The highest value
• The lowest value
2. Find out how often the numbers from 1 to 10 are generated randomly. Declare
an array to hold 10 integer elements. Generate 100 random numbers from 1 to 10
inclusive, and use the array to keep track of how many times each number occurs.
For example, if the first number generated is 9, add 1 to the 9th element of the
array. If the second number generated is 3, add 1 to the 3rd element of the array.
If the third number generated is 9, add another 1 to the 9th element of the array.
After 100 numbers have been generated, each element of the array will hold the
number of times that value was generated. Display the array.
In this example, we're using the array as a frequency table to keep track of the
frequency of each of the 10 numbers.
3. A small school is keeping track of the number of cans recycled for it's grade 1,
2, and 3 classes. Each time students bring in cans, the number of cans are counted
and added to the total for that student's class or grade.
Use an array to keep track of the totals for each of the three grades (therefore,
the array should have 3 elements). The user will be repeatedly prompted to enter
the grade number, and the number of cans brought by a student, until they
decide to quit. For each number of cans entered, add that number to the total for
that grade (e.g. the proper array element).
After the user is finished entering data, display the totals for the three grades.
4. Ask the user to enter five integer values. Store the values in an array and then
determine if the values were entered in ascending order. Display a message
indicating whether they are sorted or not.
This work is the intellectual property of Sheridan College. Any further copying and distribution outside of class
must be within the copyright law. Posting to commercial sites for profit is prohibited. © Wendi Jollymore, et al
Order of Precedence
6 + 18
2. 6 + 18 / 2
2
4.5
3. 4.5 / 12.2 - 3.1
12.2 - 3.1
Create each of the following classes based on the class diagram below (in UML,
something that starts with a + is to be declared as public; if it starts with a -
(minus), it is to be declared as private. We haven't talked about the private
modifier yet, so for now we'll declare everything as public. Note that this will
change for certain members as we learn more about OOP):
1.
The calcArea() and calcCircumference() methods calculate and return the area
and circumference of the circle (Use Google if you need to find the formulas). The
default constructor sets radius to a default value of 1. The toString() method
returns a string representation of the circle in the form:
Circle: radius=x.x
This class models a specific time stamp on a clock. The calcTotalSeconds() method
calculates and returns the total number of seconds from midnight to the time
represented by the time object (use the normal calculation for total number of
seconds: hours * 3600 + minutes * 60 + seconds). The default constructor sets the
time to midnight by setting each attribute to 0. The toString() method returns a
string representation of the time object in the form:
hh:mm:ss
(where hh, mm, and ss are the values of hours, minutes and seconds in the time
object, formatted to show exactly 2 digits)
3.
The tossDice() method generates to random integers from 1 to 6 and places one
in each instance variable. The sum method returns the sum of the two dice
values. The default constructor tosses the dice so that they are initialized with a
random set of values. The toString() method returns a string representation of the
pair of die in the form:
x, y
Test the dice class in a Main class: construct the dice and allow the user to play a
dice game that is a variation on the classic dice game called "Chicago": A player
rolls a pair of dice 11 times. In those 11 rolls, they must attempt to roll each value
from 2 to 12. At the end of the 11 rolls, total up the successful rolls to get their
score.
In each round, display the dice roll and the sum of that roll in parentheses. For
example, if the user rolled a 4 and a 3, then the value 4, 3 (7) is displayed.
After all rounds are completed, display a list of results: List each value from 2 to
12 and whether or not the user managed to roll that value during their 11 rounds.
After displaying the results of the 11 rounds, display the total points earned.
The sample program interaction and output below can better demonstrate how
the game works:
Your results:
2: yes
3: no
4: yes
5: no
6: yes
7: yes
8: yes
9: yes
10: yes
11: no
12: no
Your total score: 46
Hint: use a boolean array to keep track of which values from 2 to 12 the player
has already rolled. Each round, check to see if the array element corresponding to
that round number is set to false: if so, then that number hasn't been rolled yet,
so you can count it.
This work is the intellectual property of Sheridan College. Any further copying and distribution outside of class must be within
the copyright law. Posting to commercial sites for profit is prohibited. © Wendi Jollymore, et al
PROG10082
1. What is a method?
3. What does the following method header say about how this method works?
4. a) What is an argument?
5. Trace the program below. What is the output? Check your answer by running the program.
6. In question 5, which variables are local? To which methods are they visible?
Exercises
1. Write a program that asks the user for a sentence. Display the sentence
backwards, letter by letter. Example:
Enter a sentence:
Mary had a little lamb.
Your sentence backwards:
.bmal elttil a dah yraM
2. Write a program that asks the user for a sentence. Then ask the user which
character they'd like to replace, and which character to replace the first character
with. Create a new string with all instances of the first character replaced with the
second character, and display the new sentence on the screen. Example:
Enter a sentence:
Mary had a little lamb.
Enter a character to replace: b
Enter a character to replace it with: p
Your new sentence:
Mary had a little lamp.
3. Write a program that asks the user to enter two strings representing an
amount of time in the form hh:mm:ss. Then write the code that calculates the
total number of seconds in that amount of time. Finally, calculate and display
difference in seconds between the two times. For example:
Write a method called getTotalSeconds() that accepts a time string in the format
hh:mm:ss. This method should extract the number of hours, minutes, and
seconds from the string and then use these values to calculate the total number
of seconds. The result should be returned.
4. A program that asks the user for a sentence, and then returns that string with
the words backwards. For example:
Enter a sentence!
Mary had a little lamb.
Write a method called reverse() that accepts a sentence as a string, and then
returns that string with the words backwards. Write a main() method that gets
the string from the user and then displays the string backwards.
5. An array contains 5 strings. Write a program that creates the array (you can
hard-code the array or fill it with user inputs), then displays the index numbers of
elements that contain the letter "e" in upper-case or lower-case.
Canada's social insurance numbers (SIN) are validated using a special checksum
algorithm that goes something like this:
1. Separate the first 8 digits from the 9th digit. The 9th digit is a check-digit. Take
the remaining 8 digits:
2. Multiply the first digit by 1, the second digit by 2, the third digit by 1, the fourth
digit by 2, etc. In other words, Each odd-positioned digit (first, third, fifth, and
seventh) are multiplied by 1 and each even-positioned digit (second, fourth,
sixth, eighth) are multiplied by 2.
3. If any product above results in a 2-digit number, add those digits together to get
a single digit.
4. Take the 8 single-digit numbers from the result in step 2 and 3 and add them all
together.
5. Take the value in step 4 and find the next highest number that is evenly divisible
by 10. Subtract your step 4 solution from that number.
6. If the solution to step 5 is the 9th check-digit, then your SIN is valid. If it isn't the
same as your check-digit, then it wasn't a valid SIN.
Step 2 & 3: Multiply the odd-positioned digits by 1 and the even-positioned digits
by 2. For any result that is more than one digit, add the solution's digits together
to get a single digit.
0 * 1 = 0
4 * 2 = 8
6 * 1 = 6
4 * 2 = 8
5 * 1 = 5
4 * 2 = 8
2 * 1 = 2
8 * 2 = 16, 1 + 6 = 7
0 + 8 + 6 + 8 + 5 + 8 + 2 + 7 = 44
Step 5: Find the next highest number that is evenly divisible by 10 and subtract:
Write a program that prompts the user for a SIN and then displays whether or not
the SIN is valid or invalid.
410345678
123456030
193456787
123456789
This work is the intellectual property of Sheridan College. Any further copying and distribution outside of class
must be within the copyright law. Posting to commercial sites for profit is prohibited. © Wendi Jollymore, et al
Exercises
Create each of the following classes based on the class diagram below (in UML,
something that starts with a + is to be declared as public; if it starts with a -
(minus), it is to be declared as private. We haven't talked about the private
modifier yet, so for now we'll declare everything as public. Note that this will
change for certain members as we learn more about OOP):
1.
The calcArea() and calcCircumference() methods calculate and return the area
and circumference of the circle (Use Google if you need to find the formulas). The
default constructor sets radius to a default value of 1. The toString() method
returns a string representation of the circle in the form:
Circle: radius=x.x
hh:mm:ss
(where hh, mm, and ss are the values of hours, minutes and seconds in the time
object, formatted to show exactly 2 digits)
3.
The tossDice() method generates to random integers from 1 to 6 and places one
in each instance variable. The sum method returns the sum of the two dice
values. The default constructor tosses the dice so that they are initialized with a
random set of values. The toString() method returns a string representation of the
pair of die in the form:
x, y
Test the dice class in a Main class: construct the dice and allow the user to play a
dice game that is a variation on the classic dice game called "Chicago": A player
rolls a pair of dice 11 times. In those 11 rolls, they must attempt to roll each value
from 2 to 12. At the end of the 11 rolls, total up the successful rolls to get their
score.
In each round, display the dice roll and the sum of that roll in parentheses. For
example, if the user rolled a 4 and a 3, then the value 4, 3 (7) is displayed.
After all rounds are completed, display a list of results: List each value from 2 to
12 and whether or not the user managed to roll that value during their 11 rounds.
After displaying the results of the 11 rounds, display the total points earned.
The sample program interaction and output below can better demonstrate how
the game works:
Your results:
2: yes
3: no
4: yes
5: no
6: yes
7: yes
8: yes
9: yes
10: yes
11: no
12: no
Your total score: 46
Hint: use a boolean array to keep track of which values from 2 to 12 the player
has already rolled. Each round, check to see if the array element corresponding to
that round number is set to false: if so, then that number hasn't been rolled yet,
so you can count it.
}
This work is the intellectual property of Sheridan College. Any further copying and distribution outside of class must be within
the copyright law. Posting to commercial sites for profit is prohibited. © Wendi Jollymore, et al
Practice Problems:
1.
Design a Java program to get a number from user and find the given number is
positive or negative. Display the message.
2.
Write a Java program to calculate a Factorial of a number
3.
Write a Java program to get the age of a person and find the age group of that person as
4.
5.
Write a Java code to get a number and find whether it is prime number or not.
(note:Prime number is the number divisible by 1 and itself only)
6.
Design a Java program to print the following pattern for the positive value ‘n’.
Sample Output
7.
Write a Java program that will read in month and day (as numerical value). The
program will return the equivalent zodiac sign.
Sample output:
Enter month: 6
Enter day: 25
Zodiac sign for June 25 is Cancer
8.
Write Java code statements that accomplish the tasks listed below.
9.
Design a Java code to receive input for ‘n’ numbers and sort numbers based on user’s choice
either ascending or descending. Finally print results
10.
Design a Java program to get a string and do the following in the same program.
a. Get a character. Find the occurrence of the character from right and left side. Display
that information separately.
b. Get a positive integer from user and find the character of the index such that should
not create run time error.
11.
Design a Java program to get ‘n’ numbers and a number. Apply the linear search and binary
search. Find the best algorithm through the computation and display the result.
12.
Design a Java Program to get a matrix as input and print the transpose of the given
matrix.
13.
Develop a Java program to get a matrix and print the lower triangle of the matrix.
Apply the necessary conditions if it required.
Sample output:
14.
Get ‘n’ integer numbers from user and find the count of each unique number.
Display the result as number – its count.
Example: 3 - 5, where 3 is the number presented for 5 times)
15.
Write a Java program to get a string from user. Divide the given string into 5 equals
parts and make those part as new string. At last, print all information. Apply the
necessary conditions
16.
Design a Java Program to represent time as class which contains the following:
a. Hours, minutes and seconds as members.
b. Basic Methods to get input, print the time.
c. Method to find the difference between two times.
Demonstrate these methods through few objects. Also apply the necessary conditions
Sample Output:
17.
Design a Java program with your choice of a class to represent any real-world object.
Illustrate constructors and this keyword.
18.
Design a Student class which contains register number, name, marks for three courses,
average and result. Define methods to get input, print details, find the average and
result (if mark is more than 49 in all courses then Pass, otherwise Fail).
Keep input methods and print methods as public while others are private.
Create array of objects for the Student class and demonstrate those methods
19.
Design a class to store account details of a person like account number, name, account
type, available balance and minimum balance. Define methods to get input, display
account details, show balance, deposit and withdraw.
Apply the condition while withdraw money from account that the minimum balance to be
maintained.
Create a demo class in Java to demonstrate these methods with minimum of 3 objects
20.
Create a class Vehicle which has numOfTyres, model and company as members.
Create constructor to assign values for members, design a method to print members.
Create another class MotorVehicles which inherits Vehicle class.
Members of MotorVehicles class are price, type (Car/Bike).
Define the constructor to assign values of the members and method print which prints values of
MotorVehicles members.
Define static members to keep the count of each type of motor vehicles created.
Develop necessary methods for counting and display it at the last.
Apply the necessary mechanisms of inheritance wherever required.
Illustrate these throw a demo class Java program
21.
Write a program that displays all the leap years, seven per line, from 2001 to 2100
22.
Develop a test class to create some objects which are belong to the CourseAndMarks
class followed by getting input. Finally display all objects and the average of all three
courses. You can include some more fields other than mentioned above which must be
relevant for the process.
23.
24.
Design Java programs to serialize and deserialize the above bank account details. Find the
average balance of savings account and current account while deserialize those objects
25.
Design a Java program to store the registration number and name of students in the map. Later,
find any name is stored in the register number column too. Print the details if any name is stored
in the register number column, otherwise print “All data are correct”.
Also, print the map details.