Programming Lab 2 : classes, objects, data types and input/output

CSCI-UA 9102, Data Structures

Exercise 1

  • Exercise 1.1. Complete the class below to design a function which uses input/ouput to return the diameter of a circle, given its radius

public class Circle{
  public static void main(String [] args){

 // 1. start by declaring your variables

// 2. Display the sentence "Value of radius: " on the screen

// 3. Read the input value and store it in the radius variable

// 4. Compute the perimeter using the appropriate formula

/* 5. display the result using the sentence "The circle with radius " 
+ your value of the radius + 
"has as perimeter: "+ the perimeter you just computed */


  • Exercise 1.2. Given the function below, (1) Write down the main function that displays the result of the function f(x) for x=0, (2) Modify the main function so that it computes the result f(x) for every integer x between -5 and 5, (3) To determine the maximum value of f(x) on [-5,5], compute the value of f(x) for each of those values and store the max in a variable max

public static int f(int x){
  int resultat;
  resultat = -x*x + 3*x - 2;
  return resultat;
  • Exercise 1.3. The p-norm of a vector v = (v1,v2,…, vn) is defined as the pth root of the the sum of the components vi raised to the power p. For the special case of p=2, the gives the traditional Euclidean norm which represents the length of the vector. Give an implementation of a method named ‘norm’ such that norm(v, p) returns the p-norm of the vector v where v is represented as an array of coordinates
  • Exercise 1.4. Write an application that reads the length of the sides of a triangle from the user. Compute the area of the triangle using Heron’s formula . Print the area to 3 decimal places.
  • Exercise 1.5. Write an application that prompts for and reads the user first and last name (separately). Then print a string composed of the first letter of the user’s first name followed by the first 5 characters of the user’s last name, followed by a random number in the range 10 to 99. (Similar algorithms are sometimes used to generate usernames for new computer accounts)
  • Exercise 1.6. Write a short Java program that outputs all possible strings formed by using the characters ‘c’, ‘a’, ‘t’, ‘d’, ‘o’ and ‘g’ exactly once.
  • Exercise 1.7. Write a short Java program that takes as input three integers a,b and c and from the Java console, and determines if they can be used in a correct arithmetic formula (in the given order) like “a+b=c”, “a=b-c” or “a*b=c”
  • Exercise 1.8. Write a short Java program that can take a positive integer greater than 2 and write out the number of times one must repeatedly divide this number by 2 before getting a value less than 2.
  • Exercise 1.9. Write a short Java program that takes two arrays a and b of length n storing the int values, and returns the dot product of a and b. That is returns an array c of length n such that c[i] = a[i]*b[i], for i=1,…, n.
Exercise 2

  • Exercise 2.1. Write a short Java program that takes all the lines input to standard input and writes them to standard output in reverse order. That is each line is output in the correct order but the ordering of the lines is reversed.
  • Exercise 2.2. The birthday paradox says that the probability that two people in a room will have the same birthday is more than half, provided that n the number of people in the room is more than 23. This property is not really a paradox but many people find it surprising. Design a Java program that can test this paradox by a series of experiments on randomly generated birthdays, for n=5, 10, 15, 20, …, 100
Exercise 3

  • Exercise 3.1. Design and implement an application that reads an integer value representing a year from the user. The purpose of the program is to determine if the year is a leap year (and therefore has 29 days in February) in the Gregorian calendar. A year is a leap year if it is divisible by 4, unless it is also divisible by 100 but not 400. For example the year 2003 is not a leap year, but 2004 is. The year 1900 is not a leap year because it is divisible by 100, but the year 2000 is a leap year because even though it is divisible by 10, it is also divisible by 400. Produce an error message for any input value less than 1582 (the year the Gregorian calendar was adopted)
  • Exercise 3.2. Modify the solution to the previous exercise so that the user can evaluate multiple years. Allow the user to terminate the program using an appropriate sentinel value. Validate each input value to ensure it is greater than 1582.
  • Exercise 3.3. Design and implement an application that read an integer value and prints the sum of all even integers between 2 and and the input value, inclusive. Print an error message if the input value is less than 2. Prompt accordingly
  • Exercise 3.4. We consider the class below. Create a modified version of this class so that the spaces, punctuation, and changes in upper case and lower case are not considered when determining whether a string is a palindrome. Hint: these issues can be handled in several ways. Think carefully about your design

import java.util.Scanner;
public class PalindromeTester{
  // -----------------------------------------------
  // Test strings to see if they are palindromes 
  // -----------------------------------------------
  public static void main(String [] args){
   String str, another = "y";
   int left; right;
   Scanner scan = new Scanner(;
   while(another.equalsIgnoreCase("y")) // allows y or Y{
    System.out.println("Enter a potential palindrome: ");
    str = scan.nextLine();

    left = 0;
    right = str.length()-1;

    while(str.charAt(left) == str.charAt(right) && left < right){
    right--; }
    if(left < right) 
        System.out.println("That string is NOT a palindrome");
       System.out.println("That string IS a palindrome");
   System.out.print("Test another palindrome (y/n)? ");
   another = scan.nextLine();