Java (3415) Both Assignments
Java (3415) Both Assignments
SOLUTIONS
1. HEY THIS IS SOLUTIONS OF ASSIGNMENTS.
2. FEEL FREE SHARING THIS.
3. THE 4TH LINE IS FUNNY.
4. SUBMITTING ASSIGNMENT(S) BORROWED OR STOLEN FROM
OTHER(S) AS ONE’S OWN WILL BE NOT PENALIZED AS DEFINED IN
“AIOU STUDENTS PLAGIARISM POLICY”.
Course: Programming Language-III (3415) Semester: Spring, 2018
Level: Bachelor Pass Marks: 50
Assignment No.1
(Units 1-4)
Q.1 (a)
ANSWER
(A)
Java Application Programming
1
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
2
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
It's good practice to get into the habit of putting Java comments
into your source code to enhance its readability and clarity for
yourself and other programmers. It isn't always instantly clear
what a section of Java code is performing. A few explanatory lines
can drastically reduce the amount of time it takes to understand
the code.
Q.1 (b)
Describe the fundamentals of data types used in java with
examples.
ANSWER
Data types specify the different sizes and values that can be
stored in the variable. There are two types of data types in Java:
1. Primitive data types: The primitive data types include
Integer, Character, Boolean, and Floating Point.
2. Non-primitive data types: The non-primitive data types
include Classes, Interfaces, and Arrays.
3
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Q.2 (a)
ANSWER:
4
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
While Loop
Syntax
The syntax of a while loop is −
while (Boolean_expression) {
// Statements
}
Flow Diagram
5
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Do While Loop
The do...while loop is similar to while loop with one key difference.
The body of do...while loop is executed for once before the test
expression is checked.
Syntax
do {
} while (testExpression);
Flow Diagram
6
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
For Loop
A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to be executed a specific
number of times.
A for loop is useful when you know how many times a task is to
be repeated.
Syntax
The syntax of a for loop is −
Flow Diagram
7
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Q.2 (b)
ANSWER:
Syntax
8
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
switch (expression) {
case value:
// Statements
break; // optional
case value:
// Statements
break; // optional
9
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Programming Example 2
class SwitchBoard{
public static void main(String args[]){
int iSwitch=4;
switch(iSwitch){
case 0:
System.out.println("ZERO");
break;
case 1:
System.out.println("ONE");
break;
case 2:
System.out.println("TWO");
break;
case 3:
System.out.println("THREE");
break;
case 4:
System.out.println("FOUR");
break;
default:
System.out.println("Not in the list");
break;
}
}
}
Output:
FOUR
10
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Q.3
Write Java statements to accomplish each of the following:
(a)
Display the value of the seventh element of character
array.
ANSWER
Statement:
(b)
Total the elements of floating-point array c of 100
elements.
ANSWER
Statements:
for (int k = 0; k < 100; k++)
{
total += c[k] ;
// assume total is declared and initialized // to 0
System.out.println ( + c[k] );
}
System.out.println ( "Total is " +total );
(c)
ANSWER
11
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Statements:
double[] a = new double[11];
double[] b = new double[34]; (d)
for (int i = 0; i <= 10; i++)
Determine and print
b[i] the smallest and largest values
= a[i];
contained in 99-element floating-point array w.
(d)
ANSWER
Statement:
Q No.4
The factorial of a non-negative integer n is written n!
(Pronounced “n factorial”) and is defined as follows:
n! = n (n-1) (n-2)… 1 (for values of n greater than or
equal to 1) and n! = 1 (for n = 0).
For example, 5! = 5.4.3.2.1, which is 120.
12
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
(a)
Write an application that reads a nonnegative integer from
an input dialog and computes and prints its factorial.
ANSWER
Program Code:
import java.util.Scanner;
public class Zee2
{
public static void main(String []args)
{
Scanner input = new Scanner(System.in);
int nonNegative;
int count = 1;
int factor = 1;
System.out.println("Input a nonnegative integer: ");
nonNegative = input.nextInt();
{
while (count <= nonNegative)
{
factor = factor * count;
count++;
}
System.out.println("The factorial of the nonnegative
number is " + factor);
}
}
} (b)
Write an application that estimates the value of the
mathematical constant e by using the formula
1 1 1
e 1 ...
1! 2! 3!
13
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
ANSWER
Program Code:
if(n < 1)
{
return 0;
}
else
{
if(n == 1)
{
return 1;
}
else if(n > 1)
{
return n * fictorial(n-1);
}
}
return 0;
}
}
Q No 5
14
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
ANSWER
References
Reference Parameter
(b)
ANSWER
15
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Syntax:
Syntax:
(c)
ANSWER
Illustration:
(d)
16
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
End of 1 Assignment st
17
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
ASSIGNMENT NO.2
Question No 1
ANSWER
Inheriting Interface
18
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Inheriting Implementation
19
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
class, and the derived class uses the base class implementations
(sometimes overriding the base class methods and calling them
as part of the derived class implementations). Hierarchies
designed for interface inheritance tend to have their functionality
defined lower in the hierarchy—a base class specifies one or more
abstract methods that must be declared for each class in the
hierarchy, and the individual derived classes override these
methods to provide derived-class-specific implementations.
Q.2 (a)
What are packages in Java? Briefly describe the Java API
packages.
ANSWER
Packages in Java
There are many built-in packages such as java, lang, awt, javax,
swing, net, io, util, sql etc.
20
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
21
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Q.2 (b)
ANSWER
Casting is there to tell the compiler “I know what I’m doing, you
can trust me!” But if your code does something invalid as a
result, you risk blowing something up at runtime instead of
catching it as an error during compilation, which is a safer time to
find your problems.
22
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Q.3 (a)
Non-Abstract Methods
Q.3 (b)
23
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
has set and get methods for both length and width. The set
methods should verify that length and width are each
floating-point number larger than 0.0 and less than 20.0.
ANSWER
Program Code:
Rectangle1 Class:
package rectangle1;
public Rectangle1()
setLength(1);
setWidth(1);
setLength(iLen);
setWidth(iWid);
24
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
length = len;
width = wid;
return perimeter;
25
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
return area;
return length;
return width;
package rectangle1;
System.out.println("Rectangle 1");
System.out.printf("Perimeter: %.2f\n",r1.getPerimeter());
System.out.printf("Area: %.2f\n",r1.getArea());
26
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
System.out.println();
System.out.println();
System.out.println("Rectangle 2");
System.out.printf("Perimeter: %.2f\n",r2.getPerimeter());
System.out.printf("Area: %.2f\n",r2.getArea());
System.out.println();
System.out.println();
System.out.println("Rectangle 3");
System.out.printf("Perimeter: %.2f\n",r3.getPerimeter());
System.out.printf("Area: %.2f\n",r3.getArea());
Output:
Rectangle 1
Length: 15.00
Width: 19.00
Perimeter: 68.00
27
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Area: 285.00
Rectangle 2
Length: 2.00
Width: 8.00
Perimeter: 20.00
Area: 16.00
Rectangle 3
Length: 1.00
Width: 1.00
Perimeter: 4.00
Area: 1.00
Q.4 (a)
ANSWER
Polymorphism
28
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Q.4 (b)
Program Code
29
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
30
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Q.5
a) This Reference
b) Protected Members
d) Exception Types
ANSWER
This Reference
Protected Members
31
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
3. A class in the same package as the class with the class with
declared variable.
Exception Types
1) Built-in Exceptions
Built-in Exceptions
Arithmetic Exception
ArrayIndexOutOfBoundException
ClassNotFoundException
FileNotFoundException
IOException
InterruptedException
32
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
NoSuchFieldException
NoSuchMethodException
NullPointerException
NumberFormatException
RuntimeException
StringIndexOutOfBoundsException
User Defined Exceptions
try
{
throw new myException1();
}
catch(myException1 e)
{
System.out.println ("Exception handled - "+ e);
}
33
Prepared By: Zee K
Java Advanced Solved Assignments (3415) AIOU Spring 2018
Any suggestions…
E-mail: [email protected]
()خوند واخلئ
https://translate.google.com/#en/ps/enjoy
34
Prepared By: Zee K