Ques.1 Write a static method that builds a sorted array list from a randomly sorted array list. The method should throw an exception if the parameter's elements are not Comparable objects.
2.light travels at 3*10 meters per second. A light year is the distance a light beam would travel in one year. Write a program that calculates and displays the value of a light year.
3.Top videos rent for $3.00 a night, whereas oldies rent for $2.00. Write a program that prompts the user for the number of each type of video to rent and outputs the total cost for that night.
4.A perfect number is a positive integer such as the sum of the divisors equals the number. Thus, 28=1+2+4+7+14 is a perfect number. If the sum of the divisor is less than the number, it is deficient. If the sum exceeds the number it is abundant. Your program should define the following: 1. boolean isDivisor (int number, int divisor) 2. int divisorSum (int number) The method isDivisor returns true if the divisor parameter is a divisor of the number parameter, and false otherwise. The divisorSum method uses isDivisor to accumulate and return the sum of the proper divisors of the number parameter.
Answer to 3) int top_cost = 3; int old_cost = 2; int top = 0, old = 0; StringBuffer topBuff = new StringBuffer(); StringBuffer oldBuff = new StringBuffer();
System.out.println("Number of Top Videos: "); while (top != 10) { top = System.in.read(); if (top-48 > 0) topBuff.append(top-48); }
System.out.println("Number of Old Videos: "); while (old != 10) { old = System.in.read(); if (old-48 > 0) oldBuff.append(old-48); }
if (oldBuff.length() > 0) old = Integer.parseInt(oldBuff.toString()); else old = 0;
if (topBuff.length() > 0) top = Integer.parseInt(topBuff.toString()); else top = 0;
System.out.println("Total Cost is $" + (top * top_cost + old * old_cost));
That's really slow light you've got there. The speed of light is 300,000 km per second, not 30m per second. Experiments in the lab have slowed light down to around 57 m/sec (http://www.nature.com/nsu/030324/030324-4.html) but I think the figure you need to plug in here is the 300,000 km/sec.
4) perfect number Try this , I not sure if i really understand you request or not? write a priogram to test a perfect number? if(divisorSum(number) == number) System.out.println(number + " is a perect number"); else if(divisorSum(number) < number) System.out.println(number + " is deficient"); else if(divisorSum(number) > number) System.out.println(number + " is abundant"); System.out.print("\nSome perfect number\n\n"); for(int i =0;i<10000; i++) if(divisorSum(i) == i) System.out.print(i+" "); System.out.println(". . .\n");
} public static boolean isDivisor(int number, int divisor) { boolean div = false; if( number % divisor == 0) div = true; return div; }
public static int divisorSum(int number) { int sum = 0; for(int i = 1; i< number; i ++) { if(isDivisor(number,i) == true) sum = sum + i; } return sum; }
i suppose that you know how to read in the integer number is that right ?