1912099_CS212
1912099_CS212
SILCHAR
Branch : CSE – B
1. Write a program to count the number of vowels and consonants in a
string. Make separate methods for counting.
import java.util.*;
import java.lang.*;
class CountVowelConsonant {
++vowel;
return (vowel);
++cons;
return (cons);
input = input.toUpperCase();
22 April, 2021
int vowel = 0, cons = 0;
Scan.close();
import java.util.Scanner;
arr[j] = arr[j+1];
arr[j+1] = temp;
22 April, 2021
int[] inputs = new int[size];
inputs[i] = Scan.nextInt();
bubbleSort (inputs);
Scan.close();
import java.util.Scanner;
class Area {
public static float dimension1, dimension2;
22 April, 2021
}
void areaCircle () {
Circle objCircle = new Circle();
}
void areaRectangle () {
Rectangle objRect = new Rectangle();
}
void areaTriangle () {
Triangle objTri = new Triangle();
}
}
22 April, 2021
Ans: It has a method implementation in it. Only default and static methods have
implementations.
To correct it, we need to declare the function here and make a new class and
implement the interface and give a definition there.
SECOND
FIRST
Ans:
Given ‘a’ is an object for class A, and ‘j’ is not declared in that class,
System.out.println (a.j) will give an error message.
//*********************************//
However, assuming the code in line 12 was:
B a = new B();
22 April, 2021
7. Consider the following:
a. Which method overrides a method in the superclass?
methodTwo is capable of overriding the corresponding method in
the superclass ClassA.
Reason: An instance method overrides superclass’ instance
method.
8. If you have N eggs, then you have N/12 dozen eggs, with N%12 eggs
left over. (This essentially the definition of the / and % operators for
integers.) Write a program that asks the user how many eggs he/she
has and then tells the user how many dozen eggs he/she has and how
many extra eggs are left over. A gross of eggs is equal to 144 eggs.
Extend your program so that it will tell user how many gross, how many
dozen, and how many left over eggs he/she has. For example, if the
user says that he/she has 1342 eggs, then your program would respond
with your number of eggs is 9 gross, 3 dozen, and 10 since 1342 is equal
to 9*144 + 3*12 + 10.
import java.util.Scanner;
class Eggs {
22 April, 2021
System.out.println ("You have " + TotalEggs/12 + "
dozen of Eggs and " + TotalEggs%12 + " Eggs left over.");
else {
int LeftEggs =
TotalEggs-GrossEggs*144-DozenEggs*12;
Scan.close();
22 April, 2021
9. Which integer between 1 and 10000 has the largest number of divisors,
and how many divisors does it have? Write a program to find the
answers and print out the results. It is possible that several integers in
this range have the same, maximum number of divisors. Your program
only has to print one of them. You might need some hints about how to
find a maximum value. The basic idea is to go through all the integers,
keeping track of the largest number of divisors that you’ve seen so far.
Also, keep track of the integer that had that number of divisors.
class LrgNumOfDivisors {
public static void main (String[] args) {
int CountStore = 1, NumberStore = 1;
int Count;
int UpperBound = 10000;
for (int i=UpperBound; i>=(UpperBound-UpperBound/3);
--i) {
Count = 0;
for (int j=1; j<=i; ++j) {
if (i%j == 0)
++Count;
}
if (Count > CountStore) {
NumberStore = i;
CountStore = Count;
}
}
System.out.println ("Number " + NumberStore + " has " +
CountStore + " divisors.");
}
}
10. For this problem, you should write a very simple but complete class. The
class represents a counter that counts 0, 1, 2, 3, 4, …. The name of the
class should be Counter. It has one private instance variable
representing the value of the counter. It has two instance methods:
increment() adds one to the counter value, and getValue() returns the
current counter value. Write a complete definition for the class,
Counter.
22 April, 2021
class Counter {
++CounterValue;
return CounterValue;
22 April, 2021
12. Write a program that reads one line of input text and breaks it up into
words. The words should be output one per line. A word is defined to be
a sequence of letters. Any characters in the input that are not letters
should be discarded. For example, if the user inputs the line
He said, “That’s not a good idea.”
then the output of the program should be:
He
said
that
s
not
a
good
idea
import java.util.Scanner;
import java.lang.String;
22 April, 2021
13. Write a program to create a room class, the attributes of this class are
room_no, room_type, room_area and ACmachine (Boolean: whether
the AC is on or off). In this class the member functions are set_data and
display_data. Where set_data is used to set the attribute values and the
method display_data is used to display the same.
class Room {
room_no = num;
room_type = type;
room_area = area;
ACmachine = ac;
22 April, 2021
rum.display_data();
class SimpleObject {
SimpleObject () {
System.out.println ("Message Displayed");
}
}
15. Write a program in java to illustrate the use of “static” keyword. Create
two methods:
22 April, 2021
int result = m.multiplyNum();
System.out.println (result);
}
}
class Multiply {
private static int a = 5;
private static int b = 2;
16. Write a program to create a class named shape. In this class, we have
three subclasses: circle, triangle and square. Each class has two
member functions named draw() and erase(). Create these using
polymorphism concepts.
class Shape {
void draw() {}
void erase() {}
void draw () {
22 April, 2021
void erase() {
void draw () {
void erase () {
void draw () {
void erase () {
22 April, 2021
17. What will be the output
OUTPUT: I am a dog
22 April, 2021