CS 1073 Introductory Programming
|
1.0, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125
n = 10
**********
*********
********
*******
******
*****
****
***
**
*/b> | n = 4 **** *** ** */b> |
n = 12
************
***********
**********
*********
********
*******
******
*****
****
***
**
*/b> | n = 1 */b> |
public class Tri {
public static void chars(char ch, int n) {
for (int i = 1; i <= n; i++)
System.out.print(ch);
}
public static void main(String[] args) {
int n = 1;
// insert your answer below
}
}
// first seven prime numbers
public int[] nums = {2, 3, 5, 7, 11, 13, 17};
Write a simple code segment (with a loop) that will add up the square of each array entry. When you are done, print your answer. Thus your program should calculate 22 + 32 + 52 + 72 + 112 + 132 + 172, and it should do this by accessing the array elements. (Your program should not directly use the number 7, the number of elements in the array, but should work unchanged if one added 19 to the end of the above array.)
// Temp: temperature conversion table
public class Temp {
// ftoc: insert function definition here
public static void main(String[] args) {
for (double f = -40; f <= 220; f = f + 20) {
double c = ftoc(f);
if (f == 0) System.out.print(" ");
else if (f > 0 && f < 100) System.out.print(" ");
System.out.println(f + " " + c);
}
}
}
/* Output:
-40.0 -40.0
-20.0 -28.88888888888889
0.0 -17.77777777777778
20.0 -6.666666666666667
40.0 4.444444444444445
60.0 15.555555555555557
80.0 26.666666666666668
100.0 37.77777777777778
120.0 48.88888888888889
140.0 60.0
160.0 71.11111111111111
180.0 82.22222222222223
200.0 93.33333333333334
220.0 104.44444444444444
*/
if (f == 0) System.out.print(" ");
else if (f > 0 && f < 100) System.out.print(" ");
// Student.java: a simple class holding information about a student
public class Student {
private String name; // In the form "LastName, FirstName"
private double gpa; // grade point average
private int collegeClass; // 1 = FR, 2 = SO, 3 = JR, 4 = SR, 5 = GR
public Student( ){
}
public double getGPA() {
}
public String toString() {
}
}
// Students.java: test the Student class
public class Students {
public static void main (String[] args) {
}
}
Answer the following questions:
Write a function (method) toEnglish that will convert its int input parameter (in the range from 0 to 99 inclusive) to English. [Hints: You must use the arrays of Strings given below. First handle an input parameter value n < 20 with the teens array. Remember, if the input parameter is n = 47 then n/10 will equal 4 and you can use the tens array. Similarly n%10 will equal 7 and you can use the ones array. Then concatenate.]
String[] ones =
{"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};
String[] teens =
{"Zero", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
"Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
String[] tens =
{"", "Ten", "Twenty", "Thirty", "Forty",
"Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
public String toEnglish(int n) {
// convert n, 0 <= n <= 99, to an English string and return it
// Fill in code below.
}