CS 1073 Introductory Programming
|
| size = 7 | size = 10 | size = 15 |
|---|---|---|
*
***
*****
*******
*********
***********
***
***
***
*******
| *
***
*****
*******
*********
***********
*************
***************
*****************
***
***
***
*******
| *
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
***************************
***
***
***
******* |
public static void chars(char ch, int n) {
for (int i = 1; i <= n; i++)
System.out.print(ch);
}
The following are examples:
public static final int size = 10;
| Java Program | Three Sample runs |
|---|---|
// DemoInput: demonstrate input
import java.io.*;
public class DemoInput {
// getNextChar: fetches next char
public static char getNextChar() {
char ch;
try {
ch = (char)System.in.read();
}
catch (Exception exception)
{
System.out.println("Error reading character");
ch = ' ';
}
return ch;
}
// getNextInt: fetches next int.
public static int getNextInt() {
char ch = ' '; // = ' ' to keep compiler happy
int num; // the number to convert
int sign = 1; // gives sign of answer
ch = getNextChar();
// skip over initial blanks and newlines
while (ch == ' ' || ch == '\n')
ch = getNextChar();
// check for + or - sign
if (ch == '+') ch = getNextChar();
if (ch == '-') {
sign = -1;
ch = getNextChar();
}
// read digits, construct input integer;
// (same as the method "Integer.parseInt()" )
num = 0;
while (Character.isDigit(ch)) {
num = num*10 + (ch - '0');
ch = getNextChar();
}
return sign * num;
}
public static void main (String[] args) {
System.out.print("Enter integer ---> ");
int demoValue = getNextInt();
while (demoValue >= 0) {
System.out.println("Input value = " + demoValue);
System.out.print("Enter integer ---> ");
demoValue = getNextInt();
}
} // end of main
}
| Enter integer ---> 10 Input value = 10 Enter integer ---> 5 Input value = 5 Enter integer ---> -2 |
Note: You should use netBeans, though it is permissible to use Java from another source. In netBeans, the simplest way to make up the material to print is to just copy from netBeans (using ctrl-C) and paste into Word or WordPad (using ctrl-V). Then print the resulting document. You can assemble all 5 programs into a single document to print.
In case you have written a program for one part, but it doesn't produce output or doesn't produce the correct output, you should still turn in a listing for part credit.