0% found this document useful (0 votes)
3 views4 pages

practical_record_core_-_5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

1.

To find the sum of any number of integers entered as command line arguments
public class sum {
public static void main( String args[] ) {
int num=0;
for(int i=0; i<args.length; i++){
num+= Integer.parseInt(args[i]);
}

System.out.println("The sum is " + num);


}
}
2. To find the factorial of a given number
class FactorialExample{
public static void main(String args[]){
int i,fact=1;
int number=5;
for(i=1;i<=number;i++){
fact=fact*i; }
System.out.println("Factorial of "+number+" is: "+fact);
} }
3. To convert a decimal to binary number

import java.io.*;
class GFG
{
static void decToBinary(int n)
{
int[] binaryNum = new int[1000];
int i = 0;
while (n > 0)
{
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for (int j = i - 1; j >= 0; j--)
System.out.print(binaryNum[j]);
}
public static void main (String[] args)
{
int n = 17;
System.out.println("Decimal - " + n);
System.out.print("Binary - ");
decToBinary(n);
} }
4. To check if a number is prime or not, by taking the number as input from the keyboard
import java.util.Scanner;
class Prime {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number to check if it is truly prime number or not: ");
int number= sc.nextInt();
if(isPrime(number)) {
System.out.println(number + " is prime number"); }
else{
System.out.println(number + " is a non-prime number");
} }
static boolean isPrime(int num)
{
if(num<=1) {
return false; }
for(int i=2;i<=num/2;i++) {
if((num%i)==0)
return false; }
return true; }}
5. To find the sum of any number of integers interactively, i.e., entering every number from the
keyboard, whereas the total number of integers is given as a command line argument
public class SumCommand {
public static void main(String args[]) {
int sum = 0;
System.out.println("Calculating Sum Of Following
Number :");
for (int i = 0; i < args.length; i++) {
sum = sum + Integer.parseInt(args[i]);
System.out.println(args[i]); }
System.out.println("Sum = " + sum);
}}
6. Write a program to show that during function overloading, if no matching argument is found, then
Java will apply automatic type conversions (from lower to higher data type)
class OverloadDemo {
void test() {
System.out.println("No parameters"); }
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b); }
void test(double a) {
System.out.println("Inside test(double) a: " + a); } }
7. Write a program to show the use of static functions and to pass variable length arguments in a
function.

class Test1 {
static void fun(int... a)
{
System.out.println("Number of arguments: "
+ a.length);
for (int i : a)
System.out.print(i + " ");
System.out.println();
}
public static void main(String args[])
{
fun(100);
fun(1, 2, 3, 4);
fun();
} }
8. Write a program to demonstrate the concept of boxing and unboxing
import java.io.*;
class GFG {
public static void main(String[] args)
{ Integer i = new Integer(10);
int i1 = i;
System.out.println("Value of i:" + i);
System.out.println("Value of i1: " + i1);
Character gfg = 'a';
char ch = gfg;
System.out.println("Value of ch: " + ch);
System.out.println(" Value of gfg: " + gfg);
} }
9. Create a multi-file program where in one file a string message is taken as input from the user and the
function to display the message on the screen is given in another file

import java.io.FileWriter;
import java.io.IOException;
class Scaler{
public static void main(String[] args) {
String content = "Hello Welcome to Scaler!";
try{
FileWriter f = new FileWriter("output.txt");
f.write(content);
f.close();
System.out.println("Content is successfully added into the input.txt file."); }
catch(IOException e){
System.out.println(e); } }}
10. Write a program to create a multilevel package and also creates a reusable class to generate
Fibonacci series, where the function to generate Fibonacci series is given in a different file
belonging to the same package.

import java.util.Scanner;
public class FibSeries {
public static void main(String[] args) {
int FibLength;
Scanner sc = new Scanner(System.in); //create object
System.out.print("Please enter length: ");
FibLength = sc.nextInt();
int[] num = new int[FibLength];
//initialized first element to 0
num[0] = 0;
//initialized second element to 1
num[1] = 1;
for (int i = 2; i < FibLength; i++) {
num[i] = num[i - 1] + num[i - 2];
}
System.out.println("Fibonacci Series: ");
for (int i = 0; i < FibLength; i++) {
System.out.print(num[i] + " ");
} }}
11. Write a program that creates illustrates different levels of protection in
classes/subclasses belonging to same package or different packages
package pack1;
public class StudentData {
private String id;
private String name;
public void addStudentData(String id, String name)
{
this.id = id;
this.name = name;
}
public String getId() { return id; }
public String getName() { return name; }
}
package pack2;
import java.io.*;
import java.lang.*;
import java.util.*;
import pack1.*;
class StudentDataExtended extends StudentData { private String location;
public void addDetails(String id, String name, String location)
{
addStudentData(id, name);
this.location = location;
}
public static void
printDetails(TreeMap<String, StudentDataExtended> map)
{
for (String a : map.keySet()) {
StudentDataExtended student = map.get(a);

System.out.println(student.getId() + " " + student.getName() + " " + student.location);

} }
public static void main(String[] args)
{
System.out.print("Enter the number of students : ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String buffer = sc.nextLine();
int count = 1;
TreeMap<String, StudentDataExtended> map = new TreeMap<>();
while (n != 0) {
System.out.println( "Enter the details of Student " + count + " (id, name, location):");
count++;
String details = sc.nextLine();
String studentInfo[] = details.split(" ");
StudentDataExtended student = new StudentDataExtended();
student.addDetails(studentInfo[0], studentInfo[1], studentInfo[2]);
map.put(studentInfo[0], student);
n--;
}
System.out.println("\nThe Student Details are:");
printDetails(map);
}}

You might also like