Classes: String, coding your own class
Please make sure to include your name, Andrew ID, and the honor statement in all your code, using the format given in class.
Due Week 3: Thu by 10pm.
Note: the printed test plan for the palindrome program is due this week, (W2) Wed, before the start of lecture.
Write the following programs:
Code and use the following methods:
Your main() will prompt for a single binary number (as a string), call the convertToHex() method, and print the answer.
Have your output follow this example dialog:
Please enter a binary number: 1011
The binary number 1011 is 0xB in hex.
The Sum3 program should calculate the sum of all integers that are divisible by 3, from 1 to N. Design, code, and use a static method that takes N as an argument and uses the proper loop to return the calculated result.
Your program should prompt the user for N, print the answer, and continue prompting until the user enters -1.
Print the answer like this:
Note: Think about these questions: How is Sum3 different from 5.12 in the book? How is it the same?
Sum3 for the number 11 is: 18
Note: the Output is different from the book!
Output: instead of printing the whole table of data, show only the first 5 values and then the last five values. At the end, add 2 lines of output, in the following format:
♦ If the pattern is NOT found, print:
Where N is the number of terms.
1. Searched for 3.14159, not found after N terms.
♦ If the pattern IS found, print:
Where M is the number of terms when it first found the pattern.
1. Searched for 3.14159, found after M terms.
♦ In all cases, end the output with this:
Where N is the number of terms and result is the last value for Pi.
2. Pi for N terms is result.
A palindrome is a string of characters that reads the same forwards or backwards, e.g. "radar" is a palindrome. The program should not care if the case of the letters differ, e.g. "Dad" and "dad" are both palindromes. The program should print out the string that is to be tested and then print out whether or not the string is a palindrome (see below for the expected format of the output). Note that punctuation and whitespace are ignored when checking for a palindrome.
Design, code, and use the following methods:
Test Plan for Palindrome Program - note that you should add your own test cases. Please turn in a printed copy of your full test plan by the start of Wednesday's lecture.
Input | Output | |
toot | toot IS a palindrome. | |
dog | dog is NOT a palindrome. | |
A man, a plan, a canal, Panama! | A man, a plan, a canal, Panama! IS a palindrome. |
Note: Make sure your output matches the format shown in the above examples. So, you must print out the entered value and either IS a palindrome. or is NOT a palindrome..
Your program should continue prompting and printing right triangles until the user presses enter when prompted for the width.
To write this program, code a class called RightTriangle. It should have 3 attributes - can you figure out what they are called and what their datatypes are? Remember to make them private. Check your design with a TA by the end of Week 2.
Write the following methods in your RightTriangle class:
public void drawAsciiArt()
Call this method from your main(). This is the method that actually draws the triangle based on the values of the instance variables.
private String buildString(String str, int num)
Call this method from drawAsciiArt() to build a string that has num number of str strings in it. Use the proper loop in your code to build the resulting string.
For instance, calling buildString("|+", 3)
would return the string
"|+|+|+".