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

Assignment

The document outlines a Java programming assignment consisting of multiple tasks, including counting items in a shopping list, calculating average scores for relay teams, computing employee salaries based on attendance, reversing strings and numbers, performing basic calculator operations, summing digits, and converting data types. Each task includes a brief description and a sample program demonstrating the required functionality. The assignment aims to enhance Java programming skills through practical applications.

Uploaded by

717821i159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Assignment

The document outlines a Java programming assignment consisting of multiple tasks, including counting items in a shopping list, calculating average scores for relay teams, computing employee salaries based on attendance, reversing strings and numbers, performing basic calculator operations, summing digits, and converting data types. Each task includes a brief description and a sample program demonstrating the required functionality. The assignment aims to enhance Java programming skills through practical applications.

Uploaded by

717821i159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Java programming Assignment 1

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.

(Note : the above program is to calculate the average for 1 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:

4.Write a java program to reverse string using charAt()

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:

b.Write a java program to reverse number


Program:
import java.io.*;
import java.util.*;
class Reversenumber
{
public static void main(String []args)
{
System.out.println("enter the number");
Scanner s=new Scanner(System.in);
int num=s.nextInt();
int rem,rev=0;
while(num>0)
{
rem=num%10;
rev=(rev*10)+rem;
num=num/10;
}
System.out.println("reverse of the number:"+""+ rev);

}
}

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:

6. Write a program to perform the following task

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:

8. Print number from 1 to 50 by skipping multiples of 5 (eg., 1,2,3,4,6,7,8,9,11,12,....)

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:

9. Try to compile and execute the following for loop


​ for(;;)
System.out.println("For loop");
Program:
import java.io.*;
class Infinity{
public static void main(String arg[])
{
for(;;)
{
System.out.println("For loop");
}
}
}
Output:
10. Write a java program to perform the following conversion
a.​ String into int
b.​ Int into long
c.​ Long into int
d.​ Boolean into string
e.​ Byte into short

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:

You might also like