Back to Homework

Back to Main Page

Homework 2

Problem 1:
Write an application that converts a number of seconds into its equivalent representation as hours, minutes and seconds. The application should have a class named Convertor with a method convert (int secs). You should be able to call the convert method and the convert method should print out the results as detailed below. For instance if we wanted to convert 9999 seconds:

public class Convertor {
                    /* you will need to fill in the body of this method */
                    public void convert (int secs) {
                                        /* put your code in here */
                    }

                    public static void main (String [] args) {
                                        Convertor myConvertor = new Convertor ( );
                                        myConvertor.convert(9999);
                    }
}

Your application should output:

"9999" seconds converts to:
2 hours
46 minutes
39 seconds

where the initial number of seconds is surrounded by quotes, and the second, third and fourth lines are indented 1 tab and there is another tab after the number but before the label, i.e. (tab)2(tab)hours.

Program your convertor to convert 4237 seconds instead of the 9999 example.

Problem 2:
Write an application that takes the lengths of 3 sides of a triangle and computes the area using Heron's formula: Area = SquareRoot[s(s-a)(s-b)(s-c)] s represents 1/2 (half) the perimeter, and a,b,c are the 3 lengths. For instance if we want the area of a triangle with sides 3,4,5 we would have:

public class Triangle {
                    /*you have to fill in the body of this method*/
                    public double heron(int a, int b, int c) {
                                        double area = 0.0d;
                                        /* place code here */
                                        return area;
                    }

                    public static void main (String [] args) {
                                        Triangle myTriangle = new Triangle( );
                                        double area = myTriangle.heron (3, 4, 5);
                                        System.out.println("The area of the triangle is: "+area);
                    }
}

Your application should output:

The area of the triangle is: 6.0

Program your application to compute a triangle of sides : 31, 47, 56 instead of the 3, 4, 5 triangle in the example.

SUBMISSION INSTRUCTIONS:
Submit your two .java files to Webwork.

Copyright © College of Computing
Any unauthorized reproduction or use is strictly prohibited.