CSE 1310 - Assignments - Programming Assignment 6

The assignment will be graded out of 100 points.

Each task below will instruct you where to put your answers.

For some tasks you need to answer questions. Create a text document entitled answers.txt, or answers.docx, or answers.pdf, and put all your answers there. Acceptable file formats are plain text, Word document, OpenOffice document, and PDF. Put your name and UTA ID in the file on the first line.

Some tasks ask you to write code, and specify what name to use for the file where you save that code. You need to use exactly the name that is given (do not change the case, or make any other modification). Remember, the name of the main class must match the filename.

Some tasks specify exactly how the program output should be. Your program's output should match EXACTLY the format specified in each task. There should be no deviations, no extra spaces or lines, no extra punctuation in your output. What you see as uppercase letters should remain uppercase in your output, what you see as lowercase letters should remain as lowercase in your output, what you see as spaces and punctuation should remain exactly as spaces and punctuation in your output.

Your programs should not crash under any circumstances.


Task 1 (5 pts.)

public class task1
{
  public static int foo(int a, int b)
  {
    if (b == 0)
    {
      return 1;
    }
    return a * foo(a, b-1);
  }
          
  public static void main(String[] args)
  {
    int a = 3;
    int b = 5;
    int c = foo(b, a);
    System.out.printf("c = %s\n", c);
  }
}

If you execute this program, what will be printed? Put your answer in your answers file.


Task 2 (5 pts.)

public class task2
{
  public static int foo(int a, int b)
  {
    if (b <= 1)
    {
      return 1;
    }
    return a * bar(a, b-2);
  }
          
  public static int bar(int c, int d)
  {
    if (c <= 1)
    {
      return 1;
    }
    return d + foo(d, c);
  }
          
  public static void main(String[] args)
  {
    int a = 3;
    int b = 5;
    int c = foo(b, a);
    System.out.printf("c = %s\n", c);
  }
}
If you execute this program, what will be printed? Put your answer in your answers file.

Task 3 (10 pts.)

File task3.java contains an incomplete program. The goal of the program is to take as input strings from the user, and then print out coded (encrypted) versions of those strings, by replacing each letter with another letter. Complete that program, by defining a print_coded function, that satisfies the following specs:

Note that arguments sources and targets are hardcoded in the main function, the user cannot change those values. The user can only specify the value of word.

IMPORTANT: you are NOT allowed to modify in any way the main function.

This is an example run of the complete program:

Enter some word, or q to quit: hello
ifmmp

Enter some word, or q to quit: HELLO
HELLO

Enter some word, or q to quit: Arlington
Asmjohupo

Enter some word, or q to quit: Texas
Tfybt

Enter some word, or q to quit: q
Exiting...

Task 4 (15 pts.)

File task4.java contains an incomplete program. The goal of the program is to take as input sentences from the user, and then print out the number of words in each sentence. Complete that program, by defining a count_words function, that satisfies the following specs: The strategy for counting words is simple:
  1. You initialize your counter to zero if your text starts with a space character, or to 1 if your text starts with a letter.
  2. You go through the text, character by character. You increment your counter every time you find a letter whose previous character was space. For example, if your current character is 'm' and the previous character was ' ', it means that you have found a new word.
It may be that the text starts with multiple spaces, or that multiple spaces are placed between two words. Your code should handle that correctly.

IMPORTANT: you are NOT allowed to modify in any way the main function.

This is an example run of the complete program:

Enter some text, or q to quit: this is the fourth week of the semester
Counted 8 words.

Enter some text, or q to quit:     hello world
Counted 2 words.

Enter some text, or q to quit: 
Counted 0 words.

Enter some text, or q to quit:     h    hhg
Counted 2 words.

Enter some text, or q to quit: q
Exiting...

Task 5 (10 pts.)

File task5.java contains an incomplete program, whose goal is to take as input text from the user, and then print out that text so that each letter of the text is repeated multiple times. Complete that program, by defining a repeat_letters function, that satisfies the following specs:

IMPORTANT: you are NOT allowed to modify in any way the main function.

This is an example run of the complete program:

Enter some text, or q to quit: hello
Enter number of times (must be > 0): 3
hhheeellllllooo

Enter some text, or q to quit: good morning
Enter number of times (must be > 0): 2
ggoooodd  mmoorrnniinngg

Enter some text, or q to quit: a b
Enter number of times (must be > 0): 7
aaaaaaa       bbbbbbb

Enter some text, or q to quit: q
Exiting...

Task 6 (15 pts.)

File task6.java contains an incomplete program, that takes as input from the user a year and a month, and prints: whether the year is a leap year or not, the number of days in that year, and the number of days in that specific month of the year. Complete that program, by defining three functions.

The first function is called is_leap_year, and should satisfy the following specs:

The second function is called year_days, and should satisfy the following specs: The third function is called month_days, and should satisfy the following specs:

IMPORTANT: you are NOT allowed to modify in any way the main function.

Note: our LeapYear.java program has code that you may find useful, and you should feel free to reuse. Note that, as that program indicates, the rules for leap years are a bit more complicated than simply checking if a year is a multiple of four. More specifically, a year is a leap year if AT LEAST ONE of the following two conditions holds:

  1. The year is a multiple of 4 and the year is NOT a multiple of 100.
  2. The year is a multiple of 400.
Hint: You may find it useful to call your is_leap_year function from your year_days and month_days functions. Otherwise you need to write significantly more lines of code.

This is an example run of the complete program:

Enter a year (must be > 0): 1900
Enter a month (must be between 1 and 12): 2
Year 1900 is not a leap year.
Year 1900 has 365 days.
Month 2, 1900 has 28 days.

Enter a year (must be > 0): 2000
Enter a month (must be between 1 and 12): 2
Year 2000 is a leap year.
Year 2000 has 366 days.
Month 2, 2000 has 29 days.

Enter a year (must be > 0): 2001
Enter a month (must be between 1 and 12): 2
Year 2001 is not a leap year.
Year 2001 has 365 days.
Month 2, 2001 has 28 days.

Enter a year (must be > 0): 2005
Enter a month (must be between 1 and 12): 12
Year 2005 is not a leap year.
Year 2005 has 365 days.
Month 12, 2005 has 31 days.

Enter a year (must be > 0): q
Exiting...

Task 7 (20 pts.)

File task7.java contains an incomplete program, that takes as input from the user a date and outputs two things: the number of days that have passed from 12/31/999 up to the specified date, and the day of the week for that specified date. Complete that program, by defining two functions. The first function is called days_passed, and should satisfy the following specs: Hint: to compute that, you may find it useful to use your solutions from Task 6. Otherwise you will need to write significantly more lines of code. In more detail: The second function is called day_of_week, and should satisfy the following specs: Hint: the answer depends only on the remainder of dividing by 7 the result of days_passed(year, month, day). For example, if the remainder is 0 the function should return "Tuesday", if the remainder is 1 the function should return "Wednesday", and so on.

IMPORTANT: You are NOT allowed to modify in any way the main function..

This is an example run of the complete program:

Enter a year (must be >= 1000): 1000
Enter a month(must be between 1 and 12): 1
Enter a day: 1
1 days have passed from 12/31/999 to 1/1/1000.
1/1/1000 is a Wednesday.

Enter a year (must be >= 1000): 2015
Enter a month(must be between 1 and 12): 8
Enter a day: 4
370937 days have passed from 12/31/999 to 8/4/2015.
8/4/2015 is a Tuesday.

Enter a year (must be >= 1000): 1776
Enter a month(must be between 1 and 12): 7
Enter a day: 4
283614 days have passed from 12/31/999 to 7/4/1776.
7/4/1776 is a Thursday.

Enter a year (must be >= 1000): q
Exiting...

Task 8 (20 pts.)

File task8.java contains an incomplete program, whose goal is to allow the user to add and subtract binary numbers and see the results in binary. The user enters text such as "010101+100" or "10011-10011110", and the program prints out the result of that operation in binary. The user input can only contain two binary numbers, and one operator symbol. Function checkValid(String text) is already provided for you, and ensures that the user input is valid.

Complete the program, by defining the following five functions, which all take as argument the text that the user enters:

Hints:

Also, note that the functions binaryToDecimal and decimalToBinary that are provided to you have been extended to also handle negative numbers.

IMPORTANT: You are NOT allowed to modify in any way the main function..

This is an example run of the complete program:

Please enter your input, or q to quit: hello
Error: invalid input, please try again.

Please enter your input, or q to quit: 123+0101
Error: invalid input, please try again.

Please enter your input, or q to quit: 101+100010
operator position = 3
operation = +
the first number is 101
the second number is 100010
result = 100111

Please enter your input, or q to quit: 101-100010
operator position = 3
operation = -
the first number is 101
the second number is 100010
result = -11101

Please enter your input, or q to quit: 100010-101
operator position = 6
operation = -
the first number is 100010
the second number is 101
result = 11101

Please enter your input, or q to quit: q
Exiting...

How to submit

The assignment should be submitted via Blackboard. Submit a ZIPPED directory called assignment6.zip. No other forms of compression or submission formats will be accepted. Consult with a TA during lab hours if you do not know how to produce .zip files.

To create a zipped directory called assignment6.zip, follow these steps:

  1. Create a folder called assignment6.
  2. Copy to that folder all your solutions (your answers file, and your .java files).
  3. Zip that folder. On windows, you can zip a folder by right-clicking on the folder, and then selecting Send to->Compressed (zipped) folder.
Your zip file should contain a folder called assignment6, and that folder should contain only seven files: your answers document and the six .java files (task3.java, task4.java, task5.java, task6.java, task7.java, task8.java).

Note that, in addition to .java files, Netbeans creates several additional files and folders every time you create a project. You should NOT include those additional files in your submission.

Pay close attention to all specifications on this page, including file names and submission format. Even in cases where the program works correctly, points will be taken off for non-compliance with the instructions given on this page (such as wrong file names, wrong compression format for the submitted code, and so on).

Also, when you create your Java projects, make sure that you follow the instructions on slides 8, 9, 10 of these lectures slides: How to run a Java program. If you see a line starting with the word "package" near the top of your .java file, then you have NOT followed those instructions.

Submission checklist