Sunday, October 8, 2017

Write a program called SumAndAverage to produce the sum of 1, 2, 3, ..., to 100. Also compute and display the average.

// GO through the program and discuss What is the difference between "for" and "while-do" loops? //What is the difference between "while-do" and "do-while" loops?
/**
 * @author Sumit Srivastava
 * class calculate sum and average of number which follow some pattern
 * like number divisible by 2 or 7 in this case
 */
public class SumDigitsPattern {
int sum;
int count;
double average;

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public int oddSum(int lowerbound , int upperbound){
sum=0;
count=0;
for(int number = lowerbound ; number <=upperbound ; number++){
if(number % 2 !=0){
sum += number;
count++;
}
}
setCount(count);
return sum;
}

public double oddAverage(int lowerbound , int upperbound){
sum = oddSum(lowerbound, upperbound);
average =(double) sum / getCount();
return average;
}

public int multipleSevenSum(int lowerbound, int upperbound){
sum=0;
count=0;
for(int number = lowerbound ; number <=upperbound ; number++){
if(number % 7 ==0){
sum += number;
count++;
}
}
setCount(count);
return sum;
}

public double multipleSevenAverage(int lowerbound, int upperbound){

sum = multipleSevenSum(lowerbound, upperbound);
average =(double) sum / getCount();
return average;
}

public int sumOfSquares(int lowerbound, int upperbound){
sum=0;
count=0;
for(int number = lowerbound ; number <=upperbound ; number++){
sum += number*number;
count++;
}
setCount(count);
return sum;
}

public double averageOfSquares(int lowerbound, int upperbound){

sum = sumOfSquares(lowerbound, upperbound);
average =(double) sum / getCount();
return average;
}
}

/**
 * @author Sumit Srivastava
 * class calculate sum and average of numbers
 */
public class SumAndAverage {
public int sum; //store the accumulated sum
public double average; // store the average
int count; // store how many times loop iterates

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public int getSumUsingFor(int lowerbound , int upperbound){

for(int number = lowerbound ; number <=upperbound ; number++)
sum += number;

return sum;
}

public int getSumUsingWhile(int lowerbound , int upperbound){
int number ;
number = lowerbound;

sum =0;
while(number <= upperbound){
sum += number;
number++;
}

return sum;
}

public int getSumUsingDoWhile(int lowerbound , int upperbound){
int number ;
number = lowerbound;

sum=0;
do{
sum += number;
number++;
}while(number <= upperbound);

return sum;
}

public double getAverage(int lowerbound , int upperbound){
int totalDigits;

sum=0;
sum =getSumUsingFor(lowerbound, upperbound);
totalDigits =1 +( upperbound - lowerbound );
average =(double) sum / totalDigits;
return average;
}

public int modifiedSum(int lowerbound , int upperbound){
sum=0;
count=0;
lowerbound =111; // Overriding values of lowerbound
upperbound =8899; // Overriding values of upperbound
for(int number = lowerbound ; number <=upperbound ; number++){
sum += number;
count++;
}
setCount(count);
return sum;
}
public double modifiedAverage(int lowerbound , int upperbound){
lowerbound =111;
upperbound =8899;

sum = modifiedSum(lowerbound, upperbound);
average =(double) sum / getCount();
return average;
}
}

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
int lowerbound;
int upperbound;
lowerbound =1;
upperbound=100;
SumAndAverage sumAndAverage = new SumAndAverage();
//Printing sum using for loop
System.out.println("The sum using for loop is : "+sumAndAverage.getSumUsingFor(lowerbound, upperbound));
//Printing sum using while-do loop
System.out.println("The sum using while loop is : "+sumAndAverage.getSumUsingWhile(lowerbound, upperbound));
//Printing sum using do-while loop
System.out.println("The sum using do-while loop is : "+sumAndAverage.getSumUsingDoWhile(lowerbound, upperbound));
//Printing average
System.out.println("The average is : "+sumAndAverage.getAverage(lowerbound, upperbound));
//Printing modified Sum
System.out.println("Modified sum is : "+sumAndAverage.modifiedSum(lowerbound, upperbound));
//Printing modified Average
System.out.println("modified Average is : "+sumAndAverage.modifiedAverage(lowerbound, upperbound));
//printing how many times loop iterates to get the sum i.e value of count
System.out.println("Modified loop iterates "+sumAndAverage.getCount() +" times");
SumDigitsPattern sumDigitsPattern = new SumDigitsPattern();
//Printing sum of odd numbers
System.out.println("The sum of odd number is : "+sumDigitsPattern.oddSum(lowerbound, upperbound));
//Printing Average of odd Numbers
System.out.println("Average of odd number is : "+sumDigitsPattern.oddAverage(lowerbound, upperbound));
//Printing sum of multiple of seven
System.out.println("The sum of multiple of seven is : "+sumDigitsPattern.multipleSevenSum(lowerbound, upperbound));
//Printing average of Multiple of seven
System.out.println("The average of multiple of seven is : "+sumDigitsPattern.multipleSevenAverage(lowerbound, upperbound));
//Printing sum of squares of numbers
System.out.println("The sum of squares of number is : "+sumDigitsPattern.sumOfSquares(lowerbound, upperbound));
//Printing average of squares of numbers
System.out.println("The average of squares of number is : "+sumDigitsPattern.averageOfSquares(lowerbound, upperbound));
}
}

PrintNumberInWord (nested-if, switch-case): Write a program called PrintNumberInWord which prints "ONE", "TWO",... , "NINE", "OTHER" if the int variable "number" is 1, 2,... , 9, or other, respectively. Use (a) a "nested-if" statement; (b) a "switch-case" statement.

/**
 * @author Sumit Srivastava
 *
 */
public class PrintNumberInWord {
public String printWordUsingIf(int number) {
if(number == 1)
return "ONE" ; //return ONE if case is matched
else if(number == 2)
return "TWO"; //return TWO if case is matched
else if(number == 3)
return "THREE"; //return THREE if case is matched
else if(number == 4)
return "FOUR"; //return FOUR if case is matched
else if(number == 5)
return "FIVE"; //return FIVE if case is matched
else if(number == 6)
return "SIX"; //return SIX if case is matched
else if(number == 7)
return "SEVEN"; //return SEVEN if case is matched
else if(number == 8)
return "EIGHT"; //return EIGHT if case is matched
else if(number == 9)
return "NINE"; //return NINE if case is matched
else
return "OTHER"; //return default if no case is matched
}

public String printWordUsingSwitch(int number) {
switch(number) {
case 1 :
return "ONE";
case 2:
return "TWO";
case 3:
return "THREE";
case 4:
return "FOUR";
case 5:
return "FIVE";
case 6:
return "SIX";
case 7:
return "SEVEN";
case 8:
return "EIGHT";
case 9:
return "NINE";
default :
return "OTHER";
}
}
}

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
int number;
number = 5; // the number which you wanted to be printed in words
//Printing number in word using nested if else
PrintNumberInWord printNumberInWord = new PrintNumberInWord();
System.out.println("Printint using if else : \n "+printNumberInWord.printWordUsingIf(number));
//Printing number in word using nested Switch statement
System.out.println("Printing using Switch statement : \n "+printNumberInWord.printWordUsingSwitch(number));
}

}

Write a program called CheckPassFail which prints "PASS" if the int variable "mark" is more than or equal to 50; or prints "FAIL" otherwise.

/**
 * @author Sumit Srivastava
 * This class check that student is fail or pass
 */
public class CheckPassFail {
int CHECK = 50 ;        /*Check value input marks will be compared with this check value
i.e below 50 will be treated as fail otherwise pass*/
// this method returns true if student is pass otherwise fail
public boolean isPass(int number){
return number > CHECK;
}
}

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
int mark;
mark = 51; //input marks of the user
System.out.println("The mark is : "+mark); //Printing input marks
CheckPassFail checkPassFail = new CheckPassFail();
if(checkPassFail.isPass(mark))
System.out.println("Your status is Pass !"); //Printing the status according to condition
else
System.out.println("your status is Fail ! "); //Printing the status according to condition
}

}

Write a program called CheckOddEven which prints "Odd Number" if the int variable “number” is odd, or “Even Number” otherwise.

public class CheckOddEven {
/**
* This method will check your number i.e the number is even or not
* @return
*/
public boolean isEven(int number) {
return (number % 2 == 0);
}
}

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
int number;
number = 40; // input to check i.e input is odd or even
System.out.format("The number is : %d\n",number);
CheckOddEven checkOddEven = new CheckOddEven();
if(checkOddEven.isEven(number))
System.out.println("The number is Even");
else
System.out.println("The number is odd");
}

}

Checkboard in Java

Output Expected:

# # # # # # #
 # # # # # # #
# # # # # # #
 # # # # # # #
# # # # # # #
 # # # # # # #
# # # # # # #

Java Code:
package checkboard;

public class Main {
/**
* Main class will print checkboard pattern
*/
public static void main(String[] args) {
for(int i = 1 ; i <=7 ; i++) {
if(i %2 == 0)
System.out.print(" ");
for(int col = 1 ; col <=7 ; col++)
System.out.print("# ");
System.out.println();
}
}
}

// Hi this one is the first basic exercise to make yourself comfortable in loops
// Java Basics- Practice session- 1