Java chapter-1 practice set answer

                                                 Java chapter 1 question


Answer of all Question:

package com.ritesh;

import java.sql.SQLOutput;
import java.util.Scanner;

public class javachapter1 {
public static void main(String[] args) {
add(); //Calling the function
gpa();
enter_name();
kilometer();
detect();

}

private static void add() //question 1
{
System.out.println("Enter first number");
Scanner sc=new Scanner(System.in);
float n1=sc.nextFloat();
System.out.println("Enter second number");
Float n2=sc.nextFloat();
System.out.println("Enter Third number");
float n3= sc.nextFloat();
float output=n1+n2+n3;
System.out.println(output);

}
private static void gpa() { //question 2
System.out.println("Enter mark you obtained of java");
Scanner gpa = new Scanner(System.in);
float sub1 = gpa.nextFloat();

System.out.println("Enter mark you obtained of English");
Float sub2 = gpa.nextFloat();

System.out.println("Enter mark you obtained of Physic");
Float sub3 = gpa.nextFloat();

System.out.println("Enter mark you obtained of maths");
Float sub4 = gpa.nextFloat();

System.out.println("Enter mark you obtained of biology");
Float sub5 = gpa.nextFloat();

float totalofall = sub1 + sub2 + sub3 + sub4 + sub5;
if (totalofall<= 500) {
System.out.println("You obtained " + totalofall + " out of 500 in your exam\n");

float percentage = (totalofall / 500) * 100;
System.out.println("You obtained " + percentage + " out of 100% in your exam\n");

float gpa1 = (percentage / 100) * 4;
System.out.println("Congratulation you score " + gpa1 + " In exam\n");
}
else
System.out.println("Invalid input Please try again");
}
private static void enter_name ()//question 3
{
System.out.println("Enter you name");
Scanner sc=new Scanner(System.in);
String output=sc.nextLine();
try
{
Integer.parseInt(output);
System.out.println("Invalid keyword");
}
catch (NumberFormatException e) {
System.out.println("\"Hello<"+output+">,have a good day");
}




}
private static void kilometer() //question 4
{
System.out.println("Enter the kilometer ");
Scanner sc=new Scanner(System.in);
double kilometer=sc.nextDouble();
double covert=(kilometer/1.60934);
System.out.println("Their are "+covert+" mile in"+kilometer);
}
private static void detect() //question 5
{
System.out.println("Enter the number");
Scanner sc=new Scanner(System.in);
String in=sc.nextLine();
try
{
Integer.parseInt(in);
System.out.println(in+" Is a integer");

}
catch (NumberFormatException e) {
System.out.println(in + " Is not a integer");
}
}

}

Comments