1. Ans: I am AAnswers for Assignment 1
The program execution starts from
the main method.The first line in this method is
classclass c = new classclass();
What it actually does is
calling the constructor of classclass.Whenever an object is created , it
calls the constructor of its super class first , then it calls the constructor
in this class. In the above case there is no constructor implemented
inside the class.But every has class Object as its super class by
default. The above line of code prints nothing.
c.dummy(); is the second line of code.In side the method dummy
an object c1 of type c is declared. C c1; doesn't create an object
of type C. the statement C c2 = new C(5) creates an object of type
C.So it calls the super class constructor first and then the constructor
of this class.Super class of C is A.
So it prints Iam A . Then it calls
the constructor of class C.It has two constructors , one with no argument
and one with an int argument.Since it is new C(5) , it calls the constructor
with int argument.So it prints
I am C with 5.
2.
Ans: 10
There is a semicolon after the for loop. for(i = 0; i < 10 ; i++);
So the value of is incremented.When it comes out from the loop i = 10;
3.
Expression
Value
---------
-------
b % a
1
a < d
true
(c != b) && (a > 3)
false
a/b > c
false
a * b > 2
true
4.
e = 1
+ 1/1! + 1/ 2! + 1/3! + 1/ 4! + ..........
Ans:
public
class EValue{
//calculate the value of e .
public double calculateE(int approx){
double e = 0;
for ( int i = 0; i < approx ; i++)
e = e + 1/(double)factorial(i);
return e;
}
//calculates
the factorial of a number
private long factorial(int num){
long fact = 1;
for ( int i = 1 ; i <= num ; i++)
fact = fact * i;
return fact;
}
public static void main(String[] args){
boolean valid = false;
int terms = 0;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of terms for the e value calculation:");
while(!valid){
String n ;
try{
if((n = in.readLine()) != null)
terms = Integer.parseInt(n);
if (terms < 1)
System.out.println("Error: Enter a positive integer number greater than
zero.");
else valid = true;
}catch(IOException exp){
System.out.println("IOException :" + exp);
}
}
EValue eVal = new EValue();
double e = eVal.calculateE(terms);
System.out.println("value of e is :" + e);
}
}
5.
public class SQRTProgression
extends Progression {
protected double first;
protected double cur;
SQRTProgression(){
cur = first = 65536;
}
SQRTProgression(double start){
cur = first = start;
}
protected double first(){
cur = first;
return cur;
}
protected double next(){
cur = Maths.sqrt(cur);
return cur;
}
public void printSeries(int n){
System.out.print(first());
for(int i = 2; i <= n;i++)
System.out.print(" " + next());
System.out.println();
}
public static void main(String[] args){
SQRTProgression pro = new SQRTProgression();
System.out.println("Square Root Progression starting from 65536 :");
System.out.pritnln();
SQRTProgression pro2 = new SQRTProgression(10000);
System.out.println("Square Root Progression starting from 10000:");
pro.printProgression(10);
}
}
6.
Ans:
Read it.
Ship it.
Buy it.
Read it.
Box it.
Read it.
7.
/*In this monetary system , there are 100, 50 , 20 , 10 ,5 ,1 dollar
bills and
*coins penny(1 cent) , nickel(5
cent) , dime(10 cent) and quarter(25 cents)
*/
import java.io.*;
public class MoneyChanger{
//all the bill and
coin values are expressed in cents.
protected static int[]
value = {10000,5000,2000,1000,500,100,25,10,5,1};
public static String[]
bill = {"100 Dollars" ,"50 Dollars " , "20 Dollars" ,"10 Dollars " ,"5
Dollars ","1 Dollar","quarters","dimes","nickels","pennies"};
public int [] makeChange(double amt){
//make the amount in cents.
amt = amt * 100;
int[] result = new int[10];
for(int i = 0; i < 10; i++){
if (amt == 0)
result[i] = 0;
else {
result[i] = (int)amt/value[i];
amt = amt % value[i];
}
}
return result;
}
public static void
main(String[] args){
boolean valid = false;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String c = "",g = "";
double charged = 0,given =0;
//get the amount charged
System.out.println("Enter the monetary amount charged:");
try{
while(!valid){
if((c = in.readLine()) != null){
charged = Double.parseDouble(c);
if( charged <= 0)
System.out.println("invalid input");
else
valid = true;
}
else{
System.out.println("invalid input");
}
}
valid = false;
//get the amount given
System.out.println("Enter the amount given:");
while(!valid){
if((g = in.readLine()) != null){
given = Double.parseDouble(g);
if( given <= 0)
System.out.println("invalid input");
else
valid = true;
}
else{
System.out.println("invalid input");
}
}
}catch(IOException
exp){
System.out.println("IOException:" + exp);
}
double balance = given - charged;
if(balance
== 0)
System.out.println("No change!!!");
else if(balance < 0)
System.out.println("Given amount is less than charged amount.");
else{
System.out.println("change is :" + balance);
MoneyChanger m = new MoneyChanger();
int[] r = m.makeChange(balance);
for(int i = 0; i < 10;i++){
if(r[i] > 0)
System.out.println( r[i] + " * " + m.bill[i]
);
}
}
}
}