Tuesday, July 28, 2009

PLEASE help me with these applications for Java?? 10 pts for help.?

Do you know how I would go about setting these applications up, I know how to set up a main method, and some other basic components,...but some others I'm somewhat lost in...If you could help me type out these applications (in the Java format) I would be soooooooo grateful and consider you a complete and heroic lifesaver....so please....help me. =)





2. Write an application that reads the (x,y) coordinates for two points. Compute the distance between the two points using the following formula:


Distance = (x2 – x1)2 + (y2 – y1)2





3. Write an application that reads the radius of a sphere and printsits volume and surface area. Use the following formulas. Print the output to four decimal places, r represents the radius.


Volume = 4/3п r3


Surface area = 4п r2





4. Write an application that reads the lengths of the sides of a triangle from the user. Compute the area of the triangle using Heron's formula (below), in which 5 is half of the perimeter of the triangle, and a, b, and c are the lengths of the three sides. Print the area to three decimal places.


Area = s(s -d)(s- b)(s - c)





5. Write an application that creates and prints a random


phone number of the form XXX-XXX-XXXX. Include the dashes in the output. Do not let the first three digits contain an 8 or 9 (but don't be more restrictive than that), and make sure that the second set of three digits is not greater than 742. Hint: Think through the easiest way to construct the phone number. Each digit does not have to be determined separately.

PLEASE help me with these applications for Java?? 10 pts for help.?
2.


To read in stuff:





BufferedReader in = new BufferedReader(new InputStreamReader(System.in)));


String text = in.readLine();


try {


int number = Integer.parseInt(text);


} catch (NumberFormatException e) {


// it's not a number!


}





3.


Math.PI will give you the value of pi.


Fomatting to 4 decimal places:


public static void main(String args[]) {


float number = 34.123456789f;





DecimalFormat format = new DecimalFormat("###.####");


String formattedNumber = format.format(number);





System.out.println("original number: " + number);


System.out.println("formatted number: " + formattedNumber);


}





5.


To get a random number between 0 and 0.999999: Math.random(); It gives you a double back.





To make that random number between 0-7 (first block of phone number): int number = (int) (Math.random() * 7); Do this three times for 3 digits (maybe write a little method to give you one random number, passing in the maximum number, since you'll be making heaps):





public int generateNumber(int maxNumber) {


return (int) (Math.random() * maxNumber);


}





To make sure the second set isn't greater than 742:


int middleBit = (int) (Math.random() * 742)


No comments:

Post a Comment