Assignment solutions - Java methods
Assignment solutions - Java methods
Assignment Solutions
Assignment Solutions
Q1 - Write a Java method to compute the average of three numbers
Input:
25
45
65
Expected Output:
45
Code:
import java.util.Scanner;
int a = scn.nextInt();
int b = scn.nextInt();
int c = scn.nextInt();
System.out.print(avg(a, b, c));
return (a+b+c)/3;
coding
Output:
Expected Code:
import java.util.Scanner;
String s = scn.nextLine();
System.out.print(count(s));
int count = 0;
char ch = s.charAt(i);
count++;
return count;
Note: a) If the length of the string is even there will be two middle characters.
b) If the length of the string is odd there will be one middle character.
Input:
350
Output:
Expected Code:
import java.util.Scanner;
String s = scn.nextLine();
System.out.print(middle(s));
if(s.length() % 2 == 0){
}else{
Input:
2017
Output:
False
Expected Code:
import java.util.Scanner;
System.out.print(is_LeapYear(year));
//year is leap if it is perfectly divisible by 4, then by 100, then by 400, if not at any
step, it is not a leap year
boolean a = (y % 4) == 0;
boolean b = (y % 100) != 0;
Input:
25
37
29
Output:
25
Code:
import java.util.Scanner;
int a = scn.nextInt();
int b = scn.nextInt();
int c = scn.nextInt();
System.out.print(smallest(a, b, c));