Java Programs

  • Word Counter.

Write a method that accepts a string object as an argument and returns the number of words it contains. For instance, if the argument is "Four score and seven years ago" the method should return the number 6. Demonstrate the method in a program that asks the user to input a string and then passes it to the method. The number of words in the string should be displayed on the screen. 

Code:

package programmingChallenges;
import java.util.*;
public class WordCounter {
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
String str,str2;
int a=1,b,c=0,num;

System.out.print("Enter String: ");
str = sc.nextLine();
b = str.indexOf(' ');
while(b!=-1)
{
a++;
b= str.indexOf(' ',b+1);
}
while(str.charAt(c)==' ')
{
a=a-1;
c++;
}
System.out.println("There are "+a+" words in the string.");

System.exit(0);
}
}

  • Backward String.

Write a method that accepts a String object as an argument and displays its contents backward. For instance, if the string argument is "gravity" the method should display *yrivaig*. Demonstrate the method in a program that asks the user to input a string and then passes it to the method.

Code:

package programmingChallenges;
import java.util.*;
public class BackwardString {
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
String str,str2;
char[] ch;
int i;
System.out.print("Enter String: ");
str = sc.nextLine();

ch = str.toCharArray();

for(i=str.length()-1;i>=0;i--)
{
System.out.print(ch[i]);
}
}
}


  •  Average Rainfall


Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. First the program should ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.
Input Validation; Do not accept a number less than 1 for the number of years. Do not accept negative numbers for the monthly rainfall.

Code:

package programmingChallenge;
import java.util.*;
public class AverageRainfall {
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
int years,months = 12,totalmonths;
double month_inches,total_inches=0;
int a,b,c=1,d,e;

System.out.print("Enter Number of years: ");
years = sc.nextInt();
while(years<1 font="">
{
System.out.print("Enter a valid number of years: ");
years = sc.nextInt();
}
for(a=1;a<=years;a++)
{
for(b=1;b<=months;b++)
{
System.out.print("Enter the inches of rainfall for month no."+c+" : ");
month_inches = sc.nextDouble();
while(month_inches<0 font="">
{
System.out.print("Enter valid inches of Rainfall: ");
month_inches = sc.nextDouble();
}
total_inches += month_inches;
c++;
}
}
totalmonths = months*years;
System.out.println("Total Number of Months: \t"+totalmonths);
System.out.println("Total Inches of Rainfall:\t"+total_inches);
System.out.println("Average Rainfall per month:\t"+total_inches/totalmonths);
}

}

  • Distance Traveled 

The distance a vehicle travels can be calculated as fallout: 
Distance = Speed * Time 
For example, if a train travels 40 miles-per-hour for three hours, the distance traveled is 120 miles. Write a program that asks for the speed of a vehicle (in miles-per-hour) and the number of hours it has traveled. It should use a loop to display the distance a vehicle has traveled for each hour of a time period specified by the user. For example, if a vehicle is traveling at 40 mph for a three-hour time period, it should display a report similar to the one chat follows: 

Hour   Distance Traveled 
-------------------------------
1               40
2               80
3              120
Input Validation: Do not accept a negative number for speed and do not accept any value less than 1 for time traveled.

Code:

package programmingChallenge;
import java.util.*;
public class Question2 
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
int speed,hour,fast,time,a,b,c;
System.out.println("Enter the speed of a vehicle:");
speed = sc.nextInt();
while(speed<0 font="">
{
System.out.println("Enter a positive value for speed!");
System.out.println("Enter the speed of a vehicle: ");
speed = sc.nextInt();
}
System.out.print("Enter the number of hours it has traveled: ");
hour = sc.nextInt();
while(hour<1 font="">
{
System.out.print("Enter a valid number of hour!");
System.out.print("Enter the number of hours it has traveled:");
hour = sc.nextInt();
}
System.out.println("Hour\t Distance Traveled");
System.out.println("---------------------------");
for(a=1 ; a<=hour; a++)
{
System.out.println(a+"\t "+speed*a);
}
}
}

No comments:

Post a Comment