Lab 02, Esc101, 2004-05 Semester-II

Solve the following problems


Problem 1

/*Q1. Copy this program in Average.java file.  Run the program explain
the output you get and fix it if there is any problem in calulating
the average */

public class Average
{
	public static void main(String[] args)
	{
		double tot=0;
		System.out.println("Initial Total = " + tot);
		int n1=10;
		tot=tot + 1/n1;
		int n2=30;
		tot=tot + 1/n1;
		tot=tot*n1*n2/2;
		System.out.println("Average is " + tot);
	}
}

Problem-2

Do this problem using BlueJ. Write a class called Lab02 with a static method (with an appropriate name). The method should take a String argument, which represents a date in numeric format dd-mm-yyyy. And it should return a String in the format "Date, Month Year". For eg, given the string "06-01-2005" as argument, it should return "06, Jan 2005". The month should be the 3-letter short form ("Aug" for August, "Sep" for September, etc). Do NOT use "if-else", Java loops, or arrays for solving this problem.

Hint: For getting the month, make a very long string "Jan Feb Mar..." and from that extract the month you want. You may have to use the "substring" method of the Java String class.

To convert from a string say "12" to the corresponding integer 12, You can do it as follows:
int x = Integer.parseInt("12");
Similarly, you can also do:
int x = Integer.parseInt(s1);
where s1 is any string (assuming it has only number digits within).


Problem-3

Continuing from the previous problem, write another static method in the Lab02 class, which given the Principal value (PV), Interest Rate (INT) and number of years (YRS), returns the Final Value (FV) where FV = PV(1 + INT/100)^YRS.


Problem-4

Continuing from the previous problem, write another static method in the Lab02 class. It should take a double as argument, and print the integer part and decimal part separately in separate lines.

For e.g. given 54326.45 you have to print

54326
45

Problem-5 (bonus)

Write another static method in the Lab02 class which will take a double as argument. Assume that the number is less than 5689, and that it has not more than two digits after the decimal point. You have to print the digits of the number, one in each line (leading zeros before the decimal are ok, and trailing zeros after the decimal are ok too). And there should be a decimal point at the appropriate place too.

For example, given 2345.67, you should print

2
3
4
5
.
6
7

Again, do NOT use "if-else", Java loops, or arrays.


Bhaskaran Raman
Last modified: Thu Jan 6 14:19:34 IST 2005