Task #3: Writing void Methods
In this task, you are being asked to write void methods in Java.
Write a method called printTable() that accepts three positive integer arguments, and then prints
the table of the number from the starting number to the ending number.
You may use the following header for this method:
static void printTable(int number, int start, int end)
NOTE: Perform input validation so that all numbers must be greater than 0 and the “start” number
should be less than “end” number.
1. Create a program called TablesLab1.java.
2. Create appropriate variables and assign values using a Scanner object.
3. Correctly display appropriate messages.
import java.util.Scanner;
class TablesLab1 {
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
int number=-1;
while(number<=0){
System.out.print("Enter number: ");
number=keyBoard.nextInt();
if(number<=0){
System.out.println("The number must be greater than 0 .");
}
}
int start=-1;
while(start<=0){
System.out.print("Enter start number: ");
start=keyBoard.nextInt();
if(start<=0){
System.out.println("The start number must be greater than 0 .");
}
}
int end=start;
while(end<=start){
System.out.print("Enter end number: ");
end=keyBoard.nextInt();
if(end<=start){
System.out.println("The 'start' number should be less than 'end' number.");
}
}
printTable(number,start,end);
keyBoard.close();
}
static void printTable(int number, int start, int end){
for(int i=start;i<=end;i++){
System.out.printf("%-10d%-10d\n",i,number);
}
}
}
Comments
Leave a comment