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

Worksheet Answer 1

The document contains 13 code snippets that provide solutions to various programming problems in Java. The problems include: 1) Finding the smallest of three numbers using a ternary operator 2) Dividing two numbers and printing the float result 3) Displaying the ASCII value of a character.

Uploaded by

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

Worksheet Answer 1

The document contains 13 code snippets that provide solutions to various programming problems in Java. The problems include: 1) Finding the smallest of three numbers using a ternary operator 2) Dividing two numbers and printing the float result 3) Displaying the ASCII value of a character.

Uploaded by

Yibeltal Gashaw
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

3) Write a java program that accepts three numbers and displays the smallest number

using ternary operator.


Enter First Number: 67
Enter Second Number: 7
Enter Third Number: 9
Smallest Number is: 7
Answer:
import java.util.Scanner;
public class JavaApplication4 {
public static void main(String[] args) {
// TODO code application logic
System.out.println("Enter three numbers ");
Scanner input= new Scanner (System.in);
int num1;
int num2;
int num3;
int smallNo;
System.out.println("Enter First Number: ");
num1= input.nextInt();// store next integer in num1
System.out.println("Enter Second Number: ");
num2 = input.nextInt();// store next integer in num2
System.out.println("Enter Third Number: ");
num3 = input.nextInt();// store next integer in num3
smallNo=num1<num2?num1:num2; // checks the smallest number in and assigns
it to smaller variable
smallNo=smallNo<num3?smallNo:num3;
}

}
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);

System.out.println("IDNO\t\t\tNAME\t\t\tObtained marks\t\tToatl marks\


tPersentage");
for (j=0; j<3; j++)
{
parsentage =(ObtainedMark[j]*100)/Totalmark;
System.out.println(id[j]+"\t\t"+name[j]+"\t\t\t"+ObtainedMark[j]
+"\t\t\t"+Totalmark+"\t\t"+parsentage+"%");
}
}

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 {

public static void main(String[] args)


{
Scanner input=new Scanner(System.in);
char ch;
for(int j=0; j<26; j--)
{
System.out.print("Enter any letter: ");
ch=input.next().charAt(0);
int y= (int) ch;
if(y>=65 && y<90)
{
y+=1;
ch=(char)y;
System.out.println("Next character is "+ch);
}
if(y>=97 && y<122)
{
y+=1;
ch=(char)y;
System.out.println("Next character is "+ch);
}
if(y==90 )
{
y=65;
ch=(char)y;
System.out.println("Next character is "+ch);
}
j--;
}

You might also like