Worksheet Answer 1
Worksheet Answer 1
}
4) Write a java program to divide to numbers and print float result to the screen.
Enter the first number : 45
Enter the second number: 6
45/6 : 7.5
Answer:
package print_float_result;
import java.util.Scanner;
public class Print_float_result {
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
float value1,value2;
float result;
System.out.print("Enter first number: ");
value1=input.nextFloat();
System.out.print("Enter second number: ");
value2=input.nextFloat();
result=value1/value2;
int x= (int)value1;
int y= (int)value2;
System.out.println(x+"/"+y+"="+result);
}
}
5) Write a program to display the ASCII value of a character.
Enter any character : Z
Ascii value is : 90
Answer:
package asciivalue;
import java.util.Scanner;
public class ASCIIvalue {
public static void main(String[] args)
{
int value;
char x;
Scanner input= new Scanner(System.in);
System.out.print("Please enter any chracter ");
x=input.next().charAt(0);
value=x;
System.out.println("The ASCII value is :"+value);
System.out.println("The ASCII value is :"+x);
}
}
6) Write a java program that takes hours and minutes as input, and calculates the
total number of minutes.
Enter hours : 5
Enter minutes: 37
Total Minutes: 337
Answer:
package calculatetotalnumberofminutes;
import javax.swing.JOptionPane;
public class CalculateTotalNumberofMinutes {
public static void main(String[] args)
{
int Hour,Minute,TotalMinutes;
String s1=JOptionPane.showInputDialog("Enter hours: ");
Hour=Integer.parseInt(s1);
String s2= JOptionPane.showInputDialog("Enter minutes: ");
Minute=Integer.parseInt(s2);
TotalMinutes=(Hour*60 + Minute);
String output = "Total Minutes: " + TotalMinutes;
JOptionPane.showMessageDialog(null, output);
}
}
8) Write a java program that asks user to enter id number, name, obtained marks,
total mark of three students. It
then displays the output like this, after calculating the percentage.
IDNO NAME Obtained marks Total marks Percentage
RAMIT/0111/2013 Abebe 45 50 90%
RAMIT/0222/2013 Elsa 35 50 70%
RAMIT/0333/2013 Dawit 30 50 60%
Answer:
package calculatingpersetage;
import java.util.Scanner;
public class CalculatingPersetage {
public static void main(String[] args)
{
String id[] = new String[3];
String name[] = new String[3];
int parsentage;
int ObtainedMark[]= new int[3];
int Totalmark=50;
int i=0,j;
Scanner input=new Scanner(System.in);
do
{
System.out.println("Enter your id: ");
id[i]=input.nextLine();
System.out.println("Enter your name: ");
name[i]=input.nextLine();
System.out.println("Enter your Obtainedmark: ");
ObtainedMark[i]=input.nextInt();
input.nextLine();
i++;
}while(i<3);
9) Write a java program accepts two numbers and swaps without using a third
variable.
Before swapping
Value of num1 is: 10
Value of num2 is: 20
After swapping
Value of num1 is: 20
Value of num2 is: 10
10) Write a java program that accepts your name in lower case letters and displays
output in upper case letters.
Enter your full name : abebe kebede belete
Your name is: ABEBE KEBEDE BELETE
package guessinggame;
import java.util.Scanner;
public class Uppercase {
static Scanner input=new Scanner(System.in);
public static void main(String[] args)
{
String word;
System.out.print("Enter your full name");
word=input.nextLine();
System.out.println("Your name is: "+word.toUpperCase());
}
}
11) Write a java program to guess the generated number from the computer.
Think a number and guess the computer generated number from 1-10
Enter a guess number: 6
Wrong guess, the random number was: 8
Answer:
package guessinggame;
import java.util.Random;
import java.util.Scanner;
public class Guessinggame {
public static void main(String[] args)
{
int guess,random;
Scanner input=new Scanner(System.in);
Random gen=new Random();
random = gen.nextInt(10)+1;
System.out.println("Think a number and guess the computer generated number
from 1-10");
System.out.print("Enter a number to guess: ");
guess=input.nextInt();
if(guess==random)
{
System.out.println("Correct, You get the number");
else
{
System.out.println("Wrong guess, the random number was: "+ random);
}
}
}
13) Write a java program which accepts a character and displays its next character
Answer:
package reverse_anumber;
import java.util.Scanner;
public class Reverse_anumber {