Assignment
Assignment
1. Ram went to the department store with a list full of glossary items . The shopkeeper kept
filling up the trolley while Ram was reading out items from the list. At the end the
shopkeeper wants to know the count of items bought by Ram. Help him with that.
(Notes: create class Department and enter items through command line arguments)
Program:
import java.io.*;
class Departmental_store{
public static void main(String arg[])
{
int a;
a=arg.length;
System.out.println(a);
}
}
Output:
2. Help the Judges by calculating the scores of the teams for the intra school competition of
Relay. Since the school teams are formed randomly each house holds different numbers
of relay athletes. The school wants all athletes to participate so they decided to slightly
change the rules of Relay by allowing any numbers of players per house instead of 4 per
team. The scores for the house will be decided based on the average time taken by
athletes in each house.
Program:
import java.io.*;
class Average{
public static void main(String arg[])
{
int a,b=0,c;
int i;
a=arg.length;
for(i=0;i<a;i++)
{
b=b+Integer.parseInt(arg[i]);
}
c=b/a;
System.out.println(c);
}}
Output:
3. Help the HR team of a company by writing a program to calculate the salary of each
employee.
1) Ask employee to enter name, total working hours and leave during execution of
the class Employee
2) Add bonus/Deduct salary for the employees based on the following condition
If employees working days = company working day bonus 1000₹
If employees working days = 90% of company working day bonus 500₹
If employees working days = 80% of company working day No Bonus
If employees working days is 50-70% of company working day deduct 500₹
If employees working days <50% of company working day deduct 1000₹
Program:
import java.util.*;
import java.io.*;
class Salary{
public static void main(String arg[])
{
int work;
System.out.println("enter your name:");
Scanner s=new Scanner(System.in);
String name=s.nextLine();
System.out.println("enter total working hours:");
int n=s.nextInt();
System.out.println("enter leave:");
int l=s.nextInt();
work=n-l;
if(n==(work))
{
System.out.println("bonus is 1000");
}
else if((work)>=(n*(90/100)))
{
System.out.println("bonus is 500");
}
else if((work)>=(n*(80/100)))
{
System.out.println("bonus is no");
}
else if((work)>=(n*(50/100))||(n-1)<=(n*(70/100)))
{
System.out.println("deduct 500");
}
else
{
System.out.println("deduct 1000");
}
}}
Output:
Program:
import java.io.*;
import java.util.*;
class Revstring
{
public static void main(String[] args)
{
System.out.println("enter the string:");
Scanner s=new Scanner(System.in);
String str=s.nextLine();
String reverse=" ";
for(int i=str.length()-1;i>=0;i--)
{
reverse=reverse+str.charAt(i);
}
System.out.println("reverse string");
System.out.println(reverse);
}
}
Output:
}
}
Output:
5. Write a java program to perform basic calculator operations such as add,sub,mul and div
using switch statement , get user input during run time.
Program:
import java.io.*;
import java.util.*;
class Calculator{
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter the operator");
String c=s.nextLine();
System.out.println("enter a number");
int a=s.nextInt();
System.out.println("enter another number");
int b=s.nextInt();
switch(c)
{
case "+":
System.out.println(a+b);break;
case "-":
System.out.println(a-b);break;
case "*":
System.out.println(a*b);break;
case "/":
System.out.println(a/b);break;
}
}
}
Output:
Program:
import java.io.*;
import java.util.*;
class Sum{
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int rem,b=0;
while(n>0)
{
rem=n%10;
b=b+rem;
n=n/10;
}
System.out.printf("the sum of digit is %d",b);
}}
Output:
7. Create a array called mark and enter 5 subject mark during declaration and use enhanced for
loop to calculate sum of 5 subject mark and print the sum and average in single line using string
formatting
Program:
import java.io.*;
class Sum{
public static void main(String arg[])
{
int avg,sum=0;
int a[]=new int[]{10,20,30,40};
for(int i=0;i<4;i++){
sum+=a[i];
}
avg=sum/4;
System.out.printf("the sum is %d ",sum);
System.out.printf("the average is %d",avg);
}}
Output:
Program:
import java.io.*;
class Number{
public static void main(String arg[])
{
for(int i=0;i<=50;i++)
{
if(i%5==0)
{
continue;
}
else
{
System.out.printf("%d",i);
}
}
}
}
Output:
Program:
import java.io.*;
public class exp5 {
public static void main(String avg[]) {
String a="12";
int b=Integer.parseInt(a);
System.out.println(b);
System.out.println(((Object)b).getClass().getName());
int c=65;
long d=(long)c;
System.out.println(d);
System.out.println(((Object)d).getClass().getName());
long ai=56;
int bi=(int)ai;
System.out.println(bi);
System.out.println(((Object)bi).getClass().getName());
boolean h=true;
String g=String.valueOf(h);
System.out.println(g);
System.out.println(((Object)g).getClass().getName());
byte i=67;
short k=(short)i;
System.out.println(k);
System.out.println(((Object)k).getClass().getName());
Output: