CMPS 200 Homework Assignment 3 Solution, Fall 2012
American University of Beirut · Introduction to Programming · 10 pages · with solution
More CMPS 200 files
- Homework 11 · Fall 2018HomeworkView
- Homework Last Assignment · Fall 2018HomeworkView
- Homework Asst10 · Fall 2017HomeworkView
- Homework 9 · Spring 2017HomeworkView
- Homework Assignment 2 · Fall 2016HomeworkView
- Homework Assignment 5 · Fall 2016HomeworkView
- Homework Assignment 6 · Fall 2016HomeworkView
- Homework 1 · Spring 2016HomeworkView
Show the text of this homework
/*Ghassan Fakher
section 6
gkf01
201301739*/
public class BMI
{
public static void main(String[] args)
{
double weight = Double.parseDouble(args[0]);// weight in kilogram
double height = Double.parseDouble(args[1]);// height in meter
calculation(weight, height);
}
public static void calculation(double weight, double height) // method
that calculates bmi
{
double bmi = (weight / ( Math.pow(height, 2)));
System.out.println("BMI : " + bmi + " . Category: " +
category(bmi)); // prints the bmi and the category
}
public static String category(double bmi) //method that returns a string
showing the category of the calculated bmi
{
if (bmi < 15)
{
return ("Starvation");
}
else if (bmi < 17.5)
{
return ("Anorexic");
}
else if (bmi < 18.5)
{
return ("Underweight");
}
else if (bmi >= 18.5 && bmi < 25)
{
return ("Ideal");
}
else if (bmi >= 25 && bmi < 30)
{
return ("Overweight");
}
else if (bmi >= 30 && bmi < 40)
{
return ("Obese");
}
else if (bmi >= 40)
{
return ("Morbidly");
}
else
{
return " " ;
}
}
}
/*Ghassan Fakher
gkf01
201301739
section 6*/
public class Leap5
{
public static void main(String[] args)
{
for(int x = 1 ; x < 5 ; x++)// loops 5 times according to the number
of the given command line arguments
{
int givenYear = Integer.parseInt(args[x]);// parses the given
argument into an integer
if (!isLeap(givenYear))
{
System.out.println("The year that is not leap is : " +
args[x]); // the program will print the arguments that return a false value(not
leap years)
}
}
}
public static boolean isLeap(int year)// method that returns a true value
if year is leap and a false if not
{
return(year % 4 == 0 || year % 400 == 0 && !(year % 100 == 0)); //
the condition a leap year satisfies which is divisble by 4 and 400 but not 100
}
}
/*Ghassan Fakher
gkf01
201301739
section 6*/
public class LeapYear
{
public static void main(String[] args)
{
int year = Integer.parseInt(args[0]);
System.out.println("Is the given year leap ? " + leapYear(year)); //
prints true if the year is leap and false if not
}
public static boolean leapYear(int year)
{
return(year % 4 == 0 || year % 400 == 0 && !(year % 100 == 0)); //
condition to have a leap year
}
}
/*Ghassan Fakher
section 6
gkf01
201301739*/
public class Quadrant
{
public static void main(String[] args)
{
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
System.out.println(position(x , y)); // calls the method that tells
the position of the point of given coordinates
}
public static String position(int x, int y)
{
if(x == 0 && y == 0)
{
return("(" + x + " , " + y + ") is the origin");
}
else if(x == 0 && (y > 0 || y < 0))
{
return("(" + x + " , " + y + ") is on the y-axis");
}
else if((x < 0 || x > 0) && y == 0)
{
return("(" + x + " , " + y + ") is on the x-axis");
}
else if(x > 0 && y > 0)
{
return("(" + x + " , " + y + ") is in the first
quadrant");
}
else if(x > 0 && y < 0)
{
return("(" + x + " , " + y + ") is in the fourth
quadrant");
}
else if(x < 0 && y < 0)
{
return("(" + x + " , " + y + ") is in the third
quadrant");
}
else if(x < 0 && y > 0)
{
return("(" + x + " , " + y + ") is in the second
quadrant");
}
return " " ;
}
}
/*Ghassan Fakher
gkf01
section 6
201301739*/
public class Reverse // a program that takes the takes a certain number of
arguments from command line and prints each in reverse
{
public static void main(String[] args)
{
int n = Integer.parseInt(args[0]); // parses the first argument into
integer
for(int x = 1; x <= n; x++) // loops according to the number of the
first argument
{
String str = args[x];
printReverse(str); // calls the method printReverse that takes
string str as argument
}
}
public static void printReverse(String str) // a method that prints the in
reverse the argument found at x
{
int length = str.length();
for(int i = length - 1; i >= 0 ; i--) // loops accoording to the
length of the argument given from command line
{
System.out.print(str.charAt(i)); // prints the character at
position i
}
System.out.println(); // prints a new line after the for loop ends
}
}
/*Ghassan Fakher
gkf01
201301739
section 6 */
public class Reverse2 // program that takes a number of arguments from the
command line and prints each in reverse
{
public static void main(String[] args)
{
int numberOfArguments = Integer.parseInt(args[0]);
for(int x = 1; x <= numberOfArguments; x++) // loops according to
the integer the first argument holds
{
String str = args[x];
System.out.println(stringReverse(str));
}
System.out.println();
}
public static String stringReverse(String str) // method that takes a
string str as argument and returns a string
{
String reversed = "";
int length = str.length();
for(int i = length - 1; i >= 0; i--)
{
char c = str.charAt(i); // character c is the character of
string str at a certain position i
reversed = reversed + c; // updates the string reversed by
character c
}
return reversed;
}
}
/*Ghassan Fakher
gkf01
201301739
section 6*/
Sample runs
1) Program Reverse
PS C:\Users\ghassan\cmps200\assignment3> javac .\Reverse.java
PS C:\Users\ghassan\cmps200\assignment3> java Reverse 2 hi ghassan
ih
nassahg
2) Program Reverse2
PS C:\Users\ghassan\cmps200\assignment3> javac .\Reverse2.java
PS C:\Users\ghassan\cmps200\assignment3> java Reverse2 3 computer science
homework
retupmoc
ecneics
krowemoh
3) Program Vowels
PS C:\Users\ghassan\cmps200\assignment3> javac .\Vowels.java
PS C:\Users\ghassan\cmps200\assignment3> java Vowels COMPUTERhomework
The number of vowels in argument = 6
4) Program Quadrants
PS C:\Users\ghassan\cmps200\assignment3> javac .\Quadrant.java
PS C:\Users\ghassan\cmps200\assignment3> java Quadrant 0 0
(0 , 0) is the origin
PS C:\Users\ghassan\cmps200\assignment3> java Quadrant 4 0
(4 , 0) is on the x-axis
PS C:\Users\ghassan\cmps200\assignment3> java Quadrant 0 -3
(0 , -3) is on the y-axis
PS C:\Users\ghassan\cmps200\assignment3> java Quadrant -2 3
(-2 , 3) is in the second quadrant
PS C:\Users\ghassan\cmps200\assignment3> java Quadrant 1 2
(1 , 2) is in the first quadrant
PS C:\Users\ghassan\cmps200\assignment3> java Quadrant -2 -5
(-2 , -5) is in the third quadrant
PS C:\Users\ghassan\cmps200\assignment3> java Quadrant 4 -6
(4 , -6) is in the fourth quadrant
5)
Program LeapYear
PS C:\Users\ghassan\cmps200\assignment3> javac .\LeapYear.java
PS C:\Users\ghassan\cmps200\assignment3> java LeapYear 2015
false
6) Program Leap5
PS C:\Users\ghassan\cmps200\assignment3> javac .\Leap5.java
PS C:\Users\ghassan\cmps200\assignment3> java Leap5 5 2013 2012 2011 2010 2009
2013
2011
2010
7) Program BMI
PS C:\Users\ghassan\cmps200\assignment3> javac .\BMI.java
PS C:\Users\ghassan\cmps200\assignment3> java BMI 78 1.76
BMI : 25.180785123966942 . Category: Overweight
8) Program SumPrimes
PS C:\Users\ghassan\cmps200\assignment3> javac .\SumPrimes.java
PS C:\Users\ghassan\cmps200\assignment3> java SumPrimes 5 1 3 5 7 9
The prime number is: 3
The prime number is: 5
The prime number is: 7
15
/*Ghassan Fakher
section 6
gkf01
201301739*/
public class SumPrimes
{
public static void main(String[] args)
{
int sumOfPrimes = 0;
int num = Integer.parseInt(args[0]); // parses the first argument
into an integer
for(int i = 1; i <= num; i++) // loops according to the integer the
first argument holds
{
int number = Integer.parseInt(args[i]);
if(isPrime(number)) // if the integer is prime, it will be
printed on the screen and will update the sum by itself
{
System.out.println("The prime number is: " + number);
sumOfPrimes = sumOfPrimes + number; // updates the int
sumOfPrimes by the prime number
}
}
System.out.println("The sum of the prime numbers is : " +
sumOfPrimes); // prints out the final value of the sum of the prime number after
the for loop is done
}
public static boolean isPrime(int n)
{
int divisor = 0;
for(int i = 2; i <= n; i++) // loops from 2 till the value of the
integer the argument holds
{
if(n % i == 0) // a prime number is only divisible by one and
itself
{
divisor++; // updates the value of the divisors of the
number by 1
}
}
if (divisor == 1) // the number of divisors above 1 for a prime
number is 1
{
return true;
}
else
{
return false;
}
}
}
/*Ghassan Fakher
gkf01
201301739
section 6*/
public class Vowels// prints the number of vowels of an argument given from the
command line
{
public static void main(String[] args)
{
int numVowel = 0;
String given = args[0];// the first argument we give it from command
line
for(int position = 0; position < given.length(); position++) //
{
if (isVowel(given.charAt(position)))
{
numVowel++; // updates int numVowel by 1 if isVowel
return true
}
}
System.out.print("The number of vowels in argument = " + numVowel);
System.out.println();
}
public static boolean isVowel(char c) // returns a true or false value
{
boolean s = false;
if (c == 'a' || c == 'i' || c == 'o' || c == 'u' || c == 'e' || c ==
'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' )
{
// The vowels are a, e, i , o and u
with uppercases too
s = true;
}
return s;
}
}