Programming Lab 1 : Introduction to Java


CSCI-UA 9102, Data Structures

 
A. Getting started

In this first part, we will set up your computers to use Java. A first important tool to have whenever you start coding is a good text editor. A good choice is sublime text


A.1. If you are on Windows, follow the instructions below Once you are done with the Java installation, proceed through the following steps to install git: A.2. If you are on Mac, follow the instructions below.
B. A couple more steps..

Now that you are set up with Java and git, you will learn how to get access to the labs through github. If you do not already have one, start by opening a github account. You might also want to check that your git installation was done correctly by typing the following command

$ git --version
C. Exercise 1

In this first part, we will review the basic concepts of Java (including classes and objects, constructors, the main method, strings,…)

Exercise 1.1.

In this first part, we will review the basic concepts of Java (including classes and objects, constructors, the main method, strings,…)

  • Write a short java method that counts the number of vowels in any given string

  • Write a program that displays the word “HI” in large block letters. Make each large letter out of the other character as shown below:

  • Write a short java method that takes an integer n and returns the sum of the squares of all positive integers less than or equal to n

  • Write a short java method that takes an integer n and returns the sum of the squares of all the odd positive integers less than or equal to n

  • Write a program that prompts for and reads two integers representing the length and breadth of a rectangle, then prints its perimeter and area.

  • Using the java.util.Random class, write a program 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 655. Hint: Think through the easiest way to construct the phone number. Each digit does not have to be determined separately.

Exercise 1.2.


  • Write a public class with public constructor but a static variable. Then create several object of this class and modify the variables through the different objects. Print the resulting value after each modification.

  • Write a Java program to create a class called “Person” with a name and age attribute. Create two instances of the “Person” class, set their attributes using the constructor, and print their name and age.

  • Write a class called NumberOfGoals that represents the total number of goals scored by a football team. The NumberOfGoals class should contain a single integer as instance data, represent- ing the number of goals scored. Write a constructor to initialize the number of goals to zero. Write a method called setGoal that increments the value by one whenever a goal is scored, and another method called getGoal that returns the total num- ber of goals scored so far. Finally, create a driver class called GoalTracker that creates a few NumberOfGoals objects and tests their methods.

  • Write a class called Laptop that contains instance data for the laptop model, make, purchaser, and purchase year. Define the Laptop constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the laptop purchase. Create a driver class called LaptopRecords whose main method instantiates and updates several Laptop objects.

  • Write a class called Course that represents a course offered to students. It should contain instance data that represents the course title, course code, credits and course Instructor’s name. Define the Course constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the course. Create a driver class called CourseDetails whose main method instantiates and updates several Course objects.

  • Write a Java program to create a class called “Rectangle” with width and height attributes. Calculate the area and perimeter of the rectangle.

  • Write a Java program to create a class called “School” with attributes for students, teachers, and classes, and methods to add and remove students and teachers, and to create classes.

  • Write a complete Java program that simulates the rolling of a pair of dice. For each die in the pair, the program should generate a random number between 1 and 6 (inclusive). It should print out the result of the roll for each die and the total roll (the sum of the two dice), all appropriately labeled. You must use the Random class