Sei sulla pagina 1di 10

Programming projects Chapter # 6

PP.6.1
import java.util.Scanner;

public class evenNumbers{

public static void main(String [] args){


int number;
Scanner scan = new Scanner(System.in);
System.out.println("Enter an Integer greater than 1:");
number = scan.nextInt();
printNumber(number);
}// end main
/*declares an int variable called number and displays it on the screen*/
public static void printNumber(int number){
if (number < 2){
System.out.println("Input value must not be less than 2");
}
int sum = 2;
if(number % 2==0){
sum+= number;
}
System.out.println("Sum of even numbers between 2 and " + number + " inclusive is: " +
sum);

}//end printnumber
}
PP.6.2
import java.util.Scanner;

public class strings


{
public static void main(String[] args)
{
String message;
int marker = 0;
int length;
char current;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a string: ");
message = scan.nextLine();
length = message.length();

for (int count=0; count < length; count++)


{
current = message.charAt(marker);
System.out.println(current);
marker++;
}

}
}

PP.6.3
public class PP63 {

public static void main(String[] args) {

final int TOTAL = 12;

for(int i = 1; i <= TOTAL; i++)


{
for(int j = 1; j<=TOTAL; j++)
{
System.out.println(i*j);
}
}

}
PP.6.4
import java.util.Scanner;

public class BeerCount


{
public static void main(String[] args)
{
int numOfLines;
String another = "y";

Scanner scan = new Scanner(System.in);


while(another.equals("y"))
{
System.out.print("\nEnter the number of verses of the Beer song to be printed: ");
numOfLines = scan.nextInt();

while(numOfLines < 0 || numOfLines > 100)


{
System.out.print("Invalid input. Please enter a number between 0 and 100
(inclusive): ");
numOfLines = scan.nextInt();
}

for(int i = 100; i > 100 - numOfLines; i--)


{
System.out.println("\n" + i + " bottles of beer on the wall");
System.out.println("\n" + i + " bottles of beer");
System.out.println("\nIf one of those bottles should happen to fall");
System.out.println("\n" + (i - 1) + " bottles of beer on the wall");
}

System.out.print("Again? (y/n): ");


another = scan.nextLine();

}
}
}
PP.6.6
public class Stars
{
public static void main (String[] args)
{
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++)
{
for (int star = 10; star >= row; star--)
System.out.print ("*");
System.out.println();
}
}
}

PP.6.8
import java.util.Scanner;
public class VowelAnalyst{
public static void main(String args [])
{
int a =0, e = 0, x = 0;
int u = 0, o = 0, other = 0;
String text;
Scanner scan = new Scanner(System.in);
System.out.print("enter string: ");
text = scan.nextLine();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c=='a')
a++;
else if( c=='e')
e++;
else if(c=='i')
x++;
else if(c=='o')
o++;
else if (c=='u')
u++;
else
other++;
}

System.out.println("a: " + a + "\n" +


"e: " + e + "\n" +
"i: " + x + "\n" +
"o: " + o + "\n" +
"u: " + u + "\n" +
"other: " + other);
}
}

Potrebbero piacerti anche