﻿ Java exercises and solutions: Java if else

# Java exercises and solutions: Java if else

## Java if else

Exercise 1: Write Java program to allow the user to input his/her age. Then the program will show if the person is eligible to vote. A person who is eligible to vote must be older than or equal to 18 years old.

Enter your age: 18

You are eligible to vote.

Solution:

import java.util.*;
public class JavaExercises
{
public static void main(String[] args)
{
checkEligibility();
}

static void checkEligibility(){

int age;
Scanner sc=new Scanner(System.in);
System.out.print("What is your age?");
age=sc.nextInt();
if(age>=18)
System.out.println("You are eligible to vote.");
else
System.out.println("You are not eligible to vote.");

}

}

Exercise 2: Write a Java program to determine whether an input number is an even number.

Solution:

import java.util.*;
public class JavaExercises
{
public static void main(String[] args)
{
determineEven();
}

static void determineEven(){

int num=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number:");
num=sc.nextInt();
if(num%2==0)
System.out.println("It is an even number.");
else System.out.println("It is an odd number.");

}

}

### Comments

 lovejot aulakhVadia2018-03-19 lovejot aulakhVadia2018-03-19 lovejot aulakhVadia2018-03-19 kayo-samaang dami mong alam! -_-2017-07-10 kiooo!ewan ko jan! ^_^2017-07-10 kiooo!ewan ko jan! ^_^2017-07-10 HyuugaPart I: Consider a class Time that represents a time of day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. a) Write a default constructor that initializes the time to 0 hours, 0 minutes. b) Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the appropriate range. c) Write a method setTime(hour, minute) that sets the time if the given values are valid. d) Write another method setTime(hour, minute, isAM) that sets the time if the given values are valid. The given hour should be in the range 1 to 12. The parameter isAm is true if the time is an a. m. time and false otherwise. Part II: Complete and fully test the class Time that Part I describes. Add two more constructors that are analogous to the setTime methods described in Parts c and d of Part I. Also include the following methods: • getTime24 returns a string that gives the time in 24-hour notation hhmm. For example, if the hour value is 7 and the minute value is 25, return "0725". If the hour value is 0 and the minute value is 5, return "0005". If the hour value is 15 and the minute value is 30, return "1530". • getTime12 returns a string that gives the time in 12-hour notation h:mm xx. For example, if the hour value is 7 and the minute value is 25, return "7:25 am". If the hour value is 0 and the minute value is 5, return "12:05 am". If the hour value is 15 and the minute value is 30, return "3:30 pm".2016-02-07

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.