MIT OpenCourseWare
  • OCW home
  • Course List
  • about OCW
  • Help
  • Feedback
  • Support MIT OCW

Exercise 1: Getting Started with Java

Due: Week 2

Getting started

Make sure you have followed the instructions on the tools handout relevant to directory setup and using Java™ before you begin development.
Readings for the whole problem set are Chapters 1 through 5 of the Java Tutorial, 3rd Edition. Readings for the individual problems are listed with each problem.
If you make any assumptions in solving any problem in the exercise, please ensure that they are clearly stated in your solutions. Assumptions must be coherant and reasonable.

Problem 1: Language Basics [10 points]

Readings: Language Basics: Variables, Operators, Expressions, Statements, and Blocks

Look at the code below which calculates the radius of a circle in feet and inches, given that the circle has a given area. Download source (JAVA).



 public class MathCalc


                     {


                       public static void main(String[] args)


                     {


                       double radius = 0.0;


                       double circleArea = 100.0;


                       int feet = 0;


                       int inches = 0;


                       radius = Math.sqrt(circleArea/Math.PI);


                       feet = (int)Math.floor(radius);


                       inches = (int)Math.round(12.0*(radius - feet));


                       System.out.println("The radius of a circle with area" + circleArea 


                       + " square feet is\n " + feet + " feet " + inches + " inches");


                     }


                     }


The diameter of the Sun is approximately 865,000 miles. The diameter of the Earth is approximately 7600 miles. Use the methods in the class Math to calculate,

(a) the volume of the Earth in cubic miles
(b) the volume of the Sun in cubic miles
(c) the ratio of the volume of the Sun to the volume of the Earth

and then output the three values. Treat both the earth and sun as spheres. The volume of a sphere is given by the formula 4 pi r^3/3 where r is the radius.

Run the code you have written, and turn in both your code and the output of the program. The output should say something like: The volume of the Earth is X cubic miles, the volume of the sun is Y cubic miles, and the ratio of the volume of the Sun to the volume of the Earth is Z.

Problem 2: Language Basics [10 points]

Readings: Language Basics : Control Flow Statements



 


                   public class Primes


                     {


                      public static void main(String[] args)


                     {


                      int nValues = 50;


                      boolean isPrime = true;


                      for(int i = 2; i <= nValues; i++)


                      {


                       isPrime = true;


                       for (int j = 2; j < i; j++)


                        {


                         if (i % j == 0)


                         {


                          isPrime = false;


                          break;


                         }


                        }


                       if (isPrime)


                       System.out.println(i);


                      }


                     }


                     }


Create a new program that has the following features. Download source (JAVA).
(a) Uses labeled continue instead of break.
(b) Does not require the isPrime variable.
(c) When testing whether an integer is prime, it is sufficient to try and divide by integers up to the square root of the number being tested.

Turn in your rewritten code.

Problem 3: Object Basics [15 points]

Readings: Object Basics and Simple Data Objects, The Life Cycle of an Object,Characters and Strings

In the following code the soliloquy is analyzed character by character to determine the vowels, spaces and letters used. Fill in the code that computes the number of spaces, vowels, and consonants. Download source (JAVA).



                     public class StringCharacters


                     {


                     public static void main(String[] args)


                     {


                      String text = "To be or not to be, that is the question;"


                      +"Whether `tis nobler in the mind to suffer"


                      +" the slings and arrows of outrageous fortune,"


                      +" or to take arms against a sea of troubles,"


                      +" and by opposing end them?";


                      int spaces = 0, 


                      vowels = 0,


                      letters = 0;




                      //YOUR CODE HERE




                    System.out.println("The text contained vowels: " + vowels + "\n" +


                      consonants " + (letters - vowels) + "\n"+ spaces: " + spaces);


                     }


                     }


Run the program once you have written your code. Turn in your code, and the output produced when the code runs.

Problem 4: Object Basics [25 points]

Readings: Object Basics and Simple Data Objects

Numbers
Arrays
Write a program that sets up a String variable with the soliloquy in the previous question, extracts the words from the text and sorts them into alphabetical order. You may define 'words' however you wish (within reason), but provide your definition with your solution. You can use the sorting method of your choice. The simplest one is the bubble sort, which works as follows:

  • Starting with the first element in the array compare successive elements (0 and 1, 1 and 2, 2 and 3, and so on).
  • If the first element of any pair is greater than the second, interchange the two elements.
  • Repeat the process for the whole array until no interchanges are necessary. The array elements will now be in ascending order.
  • Run the program once you have written your code. Turn in your code, and the output produced when the code runs.

Problem 5: Classes and Inheritance [25 points]

Readings: Classes and Inheritance : Creating Classes

We define a basic class for point objects: Download source (JAVA).



                     class Point


                     {


                      double x;


                      double y;


                      


                      // Create a point from coordinates


                      Point(double xVal, double yVal)


                      {


                       x = xVal;


                       y = yVal;


                      }


                     }


Use the Point objects to define a class Line. Include a constructor to create a line from two points, a method to calculate the length of a line, and a method to convert a line to a string to print it out in readable form.

Finally, write a method intersects, called from a Line object, which returns a point as the intersection of two lines. (View resource for intersection point of two lines.)

Turn in the code that you have written. Make sure the code compiles.

Problem 6: Classes and Inheritance [15 points]

Readings: Classes and Inheritance : Managing Inheritance

Suppose we have defined a class to represent an animal as follows: Download source (JAVA).



                 public class Animal


                   {


                    public Animal(String aType)


                   {


                    type = new String(aType);


                   } 


                    public String toString()


                   {


                    return "This is a " + type;


                   } 


                    private String type;


                   }


Now define another class, based on the class Animal, to define dogs. Include private strings for the name and breed of a dog.

Define two constructors for the subclass Dog, one that just accepts an argument for the name of a dog, and other accepts both a name and breed of the Dog object. Make sure that the private base class member, type, is properly initialized.

Turn in the code you have written

Q & A

This section will list clarifications and answers to common questions about problem sets. We'll try to keep it as up-to-date as possible, so this should be the first place to look (after carefully rereading the problem set handout and the specifications) when you have a problem.

Q: Problem 2, Anonymous Comment concerning the wiseness of introducing "spaghetti code" in terms of a labeled continue (goto). Thank you very much for bringing this up the the staff so that we may address this issue.

A:

  • Jumps and branching statements (continue, exit, return, exceptions) in Java are not the same as in other languages (goto). See the Java Language Spedicifaction on Blocks and Statements. Or, see information on Unconditional Branch, which states that "Java only allows structured flow-control statements to be used and therefore does not support the Unconditional Branch."
  • Continue and exit often improve the structure of the code, certainly much better than adding lots of flags, which makes a program incomprehensible and often less efficient.
  • Read Go To Statement Considered Harmful, Edsger W. Dijkstra, and then the rebuttal by Donald Knuth, Structured Programming with go to Statements, ACM Computing Surveys, 6(4):261--301, December 1974.
  • See arguments against and for GOTOs, along with some code examples.
  • Java Developer Connection (JDC) has a tip onGOTO Statements and Java Programming, which has code comparisons between C++ (using goto) and Java™ (using labeled blocks).

Q: What Java™ coding conventions should we follow?

A: Follow the Code Conventions for the Java Programming Language. But, no points will be deducted for other reasonable coding conventions.