Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

1.There are five keywords in Java that are related only to exceptions . List them and explain what they mean. 2.Method long factorial(short n) shall calculate and return the factorial of a non-...

1 answer below »

1.There arefivekeywords in Java that are related only toexceptions. List them and explain what they mean.

2.Method long factorial(short n) shall calculate and return the factorial of a non- negative number n. If n is negative, the method shall throw a NegativeNumberException. Write a complete iterative or recursive implementation of this method.

3. Write a definition of a class that represents a scheduled airflight. (You have to come up with a good class name.) The class shall have five private data fields: short flight number, three-letter origin and destination airposrt codes (e.g., "BOS"), and departure and arrival times as byte hours (no minutes).

The class shall have a constructor that takes the five previously mentioned parameters; checks if the flight number and hours are non-negative, the airport codes differ from each other, and the departure time is before the arrival time; and initializes the data fields. If any of the conditions is false, the constructor shall throw a BadData exception.

The class shall have public method byte duration() that shall return the flight duration, in hours.

Write a small code fragments that shall create an instance of your class and print the duration of the flight.

4.What is a static variable?

5.Public class Animal has attribute short age. Public class Bear is a subclass of Animal. It imlements method void sleepAndAge() that, among other things, increments the bear's age: age++;. What should be the visibility attribute of age in the parent class for this method to be compilable? Write the complete definition of age, as you would see it in the Animal class.

Class Animal has an abstract method abstract void feed(double food);. Write the complete headers of both classes mentioned in the problem. Do not write the bodies of the classes.

6.How to make a class immutable?


7. What happens to the content of an existing writable file if the file is used with a

PrintWriter?

8. Classes Circle, Box, and Triangle are subclasses of an abstract class Shape. Declare an array of five shapes and initialize it with two circles, two triangles, and a box. (Assume that each of the classes has a constructor with no parameters.)

Class Shape has method double area(). Write a code fragment that shall display the areas of all the shapes from the previously declared and initialized array.

Answered Same Day Mar 31, 2021

Solution

Abhishek answered on Apr 01 2021
134 Votes
38194 - Java Questions/Solution.pdf
Q1. Five keywords related to Exception in Java.
Ans.
1. try: All the program that you think can raise exceptions are written inside a try block.
2. catch: This block is used to catch the exception that is thrown by the try block.
3. throw: Throw keyword is used to manually throw an exception in java.
4. throws: A exception can be thrown out of a method by using throws keyword.
5. finally: finally bolck is used to write the code that must be executed after the try block executed
is written in finally block. This finally block is executed, whether the try block throws an exception
or not.
Q2. Method long factorial(short n) shall calculate and return the factorial of a non- negative
number n. If n is negative, the method shall throw a NegativeNumberException. Write a
complete iterative or recursive implementation of this method.
Ans.
public long factorial(short n) throws NegativeNumberException
{
if(n<0)
throw new NegativeNumberException();
if (n==0)
eturn 0;
else if (n==1)
eturn 1;
else return n * factorial((short) (n-1));
}
Q3. Write a definition of a class that represents a scheduled airflight. (You have to come up
with a good class name.) The class shall have five private data fields: short flight number,
three-letter origin and destination airposrt codes (e.g., "BOS"), and departure and a
ival
times as byte hours (no minutes).
The class shall have a constructor that takes the five previously mentioned parameters; checks
if the flight number and hours are non-negative, the airport codes differ from each other, and
the departure time is before the a
ival time; and initializes the data fields. If any of the
conditions is false, the constructor shall throw a BadData exception.
The class shall have public method byte duration() that shall return the flight duration, in
hours.
Write a small code fragments that shall create an instance of your class and print the duration
of the flight
Ans.
public class ScheduledFlight {
private short flightNumber;
private String origin;
private String destination;
private byte departureTime;
private byte a
ivalTime;
public ScheduledFlight(short flightNumber, String origin, String
destination, byte departureTime, byte a
ivalTime) throws BadDataException {
super();
if(flightNumber < 0 || departureTime <0 || a
ivalTime <0)
throw new BadDataException("Flight Number, A
ival Time and
Departure time can not be negative.");
if(origin.equals(destination))
throw new BadDataException("Origin can't be same as
destination.");
if (a
ivalTime <= departureTime)
{
throw new BadDataException("Departure Time must be less than
a
ival time.");
}
this.flightNumber = flightNumber;
this.origin = origin;
this.destination = destination;
this.departureTime = departureTime;
this.a
ivalTime = a
ivalTime;
}
public byte duration()
{
eturn (byte) (a
ivalTime - departureTime);
}
}
Question 4. What is a static variable?
Ans. Static variables are the variables which are shared among all the instances of the class. They
are very useful when we want to do memory management. They are used to refer to the common
properties of all the objects of that class. The static variables gets memory only once in the class
area at the time of class loading.
Question 5. Public class Animal has attribute short age. Public class Bear is a subclass of
Animal. It imlements method void sleepAndAge() that, among other things, increments the
ear's age: age++;. What should be the visibility attribute of age in the parent class for this
method to be compilable? Write the complete definition of age, as you would see it in the
Animal class.
Class Animal has an abstract method abstract void feed(double food);. Write the complete
headers of both classes mentioned in the problem. Do not write the bodies of the classes.
Ans.
Visiblity attribute for the age variable should be protected because the subclass also has to access
this variable.
Complete definition of age: protected short age;
Header of Animal class: public abstract class Animal
Header of Bear class : public class Bear extends Animal
Question 6. How to make a class immutable?
Ans. These steps must be followedt to make a class immutable:
1. The class should be declared as a final class so that it cannot be extended.
2. All fields of the class must be made private.
3. Setter methods are not provided for this class.
4. All mutable fields are final.
5. All fields are initialized in constructoronly by deep copy.
6. Cloning of objects is done in getters to provide a copy rather than the actual reference for the
attributes.
Question 7. What happens to the content of an existing writable file if the file is used with a
PrintWriter?
Ans. If the file is used with a printwriter, all its contents are truncated to zero size and the new data
is written to it.
It simply means that all the previous data of the file is deleted and the new data is written to it.
Question 8. Classes Circle, Box, and Triangle are subclasses of an abstract class Shape.
Declare an a
ay of five shapes and initialize it with two circles, two triangles, and a box.
(Assume that each of the classes has a constructor with no parameters.)
Class Shape has method double area(). Write a code fragment that shall display the areas of
all the shapes from the previously declared and initialized a
ay.
Ans.
Class Shape:
public abstract class Shape {
public abstract double area();
}
Class Box:
public class Box extends Shape{
double height;
double width;
double length;
@Ove
ide
public double area() {
eturn 2*(height * width) + 2* (height * length) + 2*(width *
length) ;
}
}
Class Circle:
public class Circle extends Shape{
double radius;
@Ove
ide
public double area() {
eturn Math.PI * radius * radius;
}
}
Class Triangle:
public class Triangle extends Shape{
double height;
double base;
@Ove
ide
public double area() {
eturn height * base / 2;
}
}
Class MainClass:
import java.text.DecimalFormat;
public class MainClass {
public static void main(String[] args) {
Shape[] shapes=new Shape[5];
first circle
Shape circle1=new Circle();
((Circle)circle1).radius=12.3;
second circle
Shape circle2=new Circle();
((Circle)circle2).radius=6.3;
first triangle
Shape triangle1=new Triangle();
((Triangle)triangle1).height=1.3;
((Triangle)triangle1).base=5.3;
second triangle
Shape triangle2=new Triangle();
((Triangle)triangle2).height=6.3;
((Triangle)triangle2).base=2.3;
box
Shape box = new Box();
((Box)box).height=1.3;
((Box)box).length=3.3;
((Box)box).width=7.3;
shapes[0]= circle1;
shapes[1]=circle2;
shapes[2]=triangle1;
shapes[3]=triangle2;
shapes[4]=box;
DecimalFormat format= new DecimalFormat("#0.00");
for(int i=0;i<5;i++)
{
System.out.println(shapes[i].getClass()+" Area "+(i+1)+":
"+format.format(shapes[i].area()));
}
}
}
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here