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:
- Function print_coded takes three arguments, called word, sources, targets. They are all strings.
- Function print_coded processes the letters
of word one by one, in the order in which they appear in word. For every such letter X, the function processes X as follows:
- If X is equal to the character at position P of sources, then the function prints the character at position P of targets.
- Otherwise, the function prints X.
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:
- Function count_words takes one argument, called text, which is a string, and which is allowed to contain only uppercase letters, lowercase letters, and the space character ' '. In other words, don't worry about handling cases cases where text contains characters that are punctuation, special symbols, or anything else that is not a letter or the space character.
- Function count_words counts and returns the number of words in text.
The strategy for counting words is simple:
- You initialize your counter to zero if your text starts with a space character, or to 1 if your text starts with a letter.
- 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:
- repeat_letters takes two arguments, called text, times.
- The function goes through the letters of text in the order in which they appear in text, and prints each such letter as many times as specified by the argument times.
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:
- is_leap_year takes one argument, called year, which is an integer greater than 0 (we will only test your code with such values).
- If year is a leap year, then the function returns boolean value true.
- Otherwise, year_days(year) returns false.
The second function is called year_days, and should satisfy the following specs:
- year_days takes one argument, called year, which is an integer greater than 0 (we will only test your code with such values).
- If year is a leap year, then year_days(year) returns 366.
- Otherwise, year_days(year) returns 365.
The third function is called month_days, and should satisfy the following specs:
- month_days takes two arguments, called year, month. Argument year is an integer greater than 0 (we will only test your code with such values).
The month argument is an integer between 1 and 12 (we will only test your code with such values).
- The function returns the number of days that the month lasts for. Unless the month is 2 (for February), the result does not depend on the year. If the month is 2, then obviously the result should be 28 or 29, depending on whether the year is a leap year.
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:
- The year is a multiple of 4 and the year is NOT a multiple of 100.
- 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:
- Function days_passed takes three arguments, called year, month, day. You can assume that year > 0, that month is between 1 and 12, and that dayis between 1 and the number of days for that month of that year. We will only test your code with such cases.
- The function returns the number of days that have passed from 12/31/999 to the specified date.
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:
- Do a for-loop from 1000 up to (and not including) the specified year, adding up the days of each year in the loop. You may find it useful to call your year_days function from Task 6 (to be able to call your year_days function, you need to copy and paste that function from your task6.java file to your task7.java file).
- Then, do another for-loop, from 1 up to (and not including) the specified month, adding up the days of each month in the loop. You may find it useful to call your month_days function from Task 6 (to be able to call your month_days function, you need to copy and paste that function from your task6.java file to your task7.java file).
- Finally, add to your total the specified day of the month.
The second function is called day_of_week, and should satisfy the following specs:
- Function day_of_week takes three arguments, called year, month, day. For these arguments, you can make the same assumptions that are stated for the arguments of the days_passed function.
- The function returns the day of the week (e.g, Sunday, Tuesday, and so on) that corresponds to the specified date.
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:
- Function int findOperatorPosition(String text) returns the position in text where the operator symbol (+ or -) occurs. For example, findOperatorPosition("1011-10010") returns 4.
- Function int findOperatorSymbol(String text) returns the operator symbol in text, which is either the character '+' or the character '-'. For example, findOperatorSymbol("1011-10010") returns '-'.
- Function String getFirstNumber(String text) returns the first binary number in text. For example, getFirstNumber("1011-10010") returns "1011".
- Function String getSecondNumber(String text) returns the second binary number in text. For example, getSecondNumber("1011-10010") returns "10010".
- Function String computeBinaryOperation(String text) returns the result of the operation described by text, as a binary number. For example, getSecondNumber("1011-10010") returns "-111".
Hints:
- The main goal of this task is to get you to write function computeBinaryOperation. The other four functions essentially solve smaller tasks that function computeBinaryOperation needs to perform.
- My solutions for getFirstNumber and getSecondNumber call function findOperatorPosition to figure out which part of text contains the number they want to extract.
- My solution for computeBinaryOperation goes through these steps:
- It calls functions getFirstNumber and getSecondNumber to get the two binary numbers as strings.
- It calls function binaryToDecimal to convert each binary number to a decimal integer (which is a regular Java int).
- It calls function findOperatorSymbol to find whether the operator is addition or subtraction.
- Depending on the operator, it performs addition or subtraction of the two numbers.
- It calls function decimalToBinary to convert the result of the operation into a binary number.
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:
- Create a folder called assignment6.
- Copy to that folder all your solutions (your answers file, and your .java files).
- 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
- Make sure that your code runs without any compilation errors. There should be no warning from Netbeans about compilation errors when you start any of your programs.
- 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 a Java file, then you have NOT followed those instructions.
- Make sure your zip file includes your answers file, with your name, UTA ID, and answers to non-programming tasks.
- In addition to your answers file, make sure your zip file includes task3.java, task4.java, task5.java, task6.java, task7.java, task8.java. Do not submit any additional files or folders.
- After you have submitted, download your submission, and doublecheck to make sure that it contains the right files with the right content.