0% found this document useful (0 votes)
3 views

Assignment solutions - Java methods

The document contains solutions to five Java programming assignments, each demonstrating a specific method. The assignments include calculating the average of three numbers, counting vowels in a string, displaying the middle character of a string, checking for leap years, and finding the smallest number among three inputs. Each solution is accompanied by example inputs and expected outputs, along with the corresponding Java code.

Uploaded by

Shreya Mehta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment solutions - Java methods

The document contains solutions to five Java programming assignments, each demonstrating a specific method. The assignments include calculating the average of three numbers, counting vowels in a string, displaying the middle character of a string, checking for leap years, and finding the smallest number among three inputs. Each solution is accompanied by example inputs and expected outputs, along with the corresponding Java code.

Uploaded by

Shreya Mehta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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;

public class Test{

public static void main(String[] args){

Scanner scn = new Scanner(System.in);

System.out.println(“Enter the three numbers: ”);

int a = scn.nextInt();

int b = scn.nextInt();

int c = scn.nextInt();

System.out.print(avg(a, b, c));

public static int avg(int a, int b, int c){

return (a+b+c)/3;

Cracking the Coding Interview in JAVA - Foundation


Assignment Solutions
Q2 - Write a Java method to count all vowels in a string

Input: (consists of all lowercase letters)

coding

Output:

Expected Code:

import java.util.Scanner;

public class Test{

public static void main(String[] args){

Scanner scn = new Scanner(System.in);

System.out.println(“Enter the string: ”);

String s = scn.nextLine();

System.out.print(count(s));

public static int count(String s){

int count = 0;

for(int i = 0; i < s.length(); i++){

char ch = s.charAt(i);

if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){

count++;

return count;

Cracking the Coding Interview in JAVA - Foundation


Assignment Solutions
Q3 - Write a Java method to display the middle character of a string.

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;

public class Test{

public static void main(String[] args){

Scanner scn = new Scanner(System.in);

System.out.println(“Enter the string: ”);

String s = scn.nextLine();

System.out.print(middle(s));

public static String middle(String s){

if(s.length() % 2 == 0){

return s.substring(s.length()/2, s.length()/2 + 2);

}else{

return s.substring(s.length()/2, s.length()/2 + 1);

Cracking the Coding Interview in JAVA - Foundation


Assignment Solutions
Q4 - Write a Java method to check whether a year (integer) entered by the user is a leap year or not.

Input:

2017

Output:

False

Expected Code:

import java.util.Scanner;

public class Test{

public static void main(String[] args){

Scanner scn = new Scanner(System.in);

System.out.println(“Enter the year: ”);

int year = scn.nextInt();

System.out.print(is_LeapYear(year));

public static boolean is_LeapYear(int y){

//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;

boolean c = ((y % 100 == 0) && (y % 400 == 0));

return a && (b || c);

Cracking the Coding Interview in JAVA - Foundation


Assignment Solutions
Q5 - Write a Java method to find the smallest number among three numbers.

Input:

25

37

29

Output:

25

Code:

import java.util.Scanner;

public class Test{

public static void main(String[] args){

Scanner scn = new Scanner(System.in);

System.out.println(“Enter the three numbers: ”);

int a = scn.nextInt();

int b = scn.nextInt();

int c = scn.nextInt();

System.out.print(smallest(a, b, c));

public static int smallest(int a, int b, int c){

return Math.min(a, Math.min(b,c));

Cracking the Coding Interview in JAVA - Foundation

You might also like