The Artima Developer Community
Sponsored Link

Java Answers Forum
pls help I'm desparate - still new in Java

5 replies on 1 page. Most recent reply: Jul 2, 2003 6:45 AM by KevinT

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Sello

Posts: 3
Nickname: comic123
Registered: Jul, 2003

pls help I'm desparate - still new in Java Posted: Jul 1, 2003 3:52 AM
Reply to this message Reply
Advertisement
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.


balaji

Posts: 2
Nickname: krishbala
Registered: Jul, 2003

Re: pls help I'm desparate - still new in Java Posted: Jul 1, 2003 7:29 AM
Reply to this message Reply
Answer to 2)
long meters_per_second = 30;
System.out.println("Number of Light Years: ");
int no_of_years = System.in.read();
System.out.println("Distance traveled is " + ((long)(meters_per_second * 3600 * 24 * 365 * no_of_years)));

balaji

Posts: 2
Nickname: krishbala
Registered: Jul, 2003

Re: pls help I'm desparate - still new in Java Posted: Jul 1, 2003 7:50 AM
Reply to this message Reply
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));

Sello

Posts: 3
Nickname: comic123
Registered: Jul, 2003

Re: pls help I'm desparate - still new in Java Posted: Jul 2, 2003 12:13 AM
Reply to this message Reply
thanks a million man Balaji, you are a lifesaver.

Paul Umbers

Posts: 7
Nickname: paulu
Registered: May, 2003

Re: pls help I'm desparate - still new in Java Posted: Jul 2, 2003 6:13 AM
Reply to this message Reply
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.

KevinT

Posts: 24
Nickname: metzedler
Registered: Jun, 2003

Re: pls help I'm desparate - still new in Java Posted: Jul 2, 2003 6:45 AM
Reply to this message Reply
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 ?

Flat View: This topic has 5 replies on 1 page
Topic: create many variably named components? Previous Topic   Next Topic Topic: Reverse polish using a tokenizer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use