TOLANI FOUNDATION GANDHIDHAM POLYTECHNIC
COMPUTER DEPARTMENT
JAVA E-ASSIGNMENT
Name:-Naimish Hadiya
Enrollment No:-216510307153
1.
import java.util.Scanner;
class Q1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first value :: ");
int a = sc.nextInt();
System.out.print("Enter the second value :: ");
int b = sc.nextInt();
int add = a + b;
System.out.println("Addition is :: "+add);
int sub = a - b;
System.out.println("Subtraction is :: "+ sub);
int mul = a * b;
System.out.println("Multiplication is " + mul);
int div = a / b;
System.out.println("Division is :: " + div);
int mod = a % b;
System.out.println("Modulo is :: " + mod);
}
}
Output:-
Enter the first value :: 10
Enter the second value :: 20
Addition is :: 30
Subtraction is :: -10
Multiplication is 200
Division is :: 0
Modulo is :: 10
2.
class Q2
{
public static void main(String args[])
{
for(int i=0;i<args.length;i++)
{
System.out.println(args[i]);
}
}
}
Output:-
java Q2 10 20 30 40 50
10
20
30
40
50
3.
import java.util.Scanner;
class Q3
{
int fact(int n)
{
if(n == 0)
{
return 1;
}
else
{
return n * fact(n - 1);
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
Q3 obj = new Q3();
System.out.print("Enter a value :: ");
int n =sc.nextInt();
System.out.println("Factorial is : " + obj.fact(n));
}
}
Output:-
Enter a value :: 5
Factorial is : 120
4.
import java.util.Scanner;
class Q4
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int[] arr = new int[20];
int sum = 0;
int product = 1;
System.out.print("Enter size of array :: ");
int n = sc.nextInt();
for(int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
for (int i = 0; i < 5; i++)
{
sum += arr[i];
product *= arr[i];
}
System.out.println("Sum is :: " + sum);
System.out.println("Product is :: " + product);
}
}
Output:-
Enter size of array :: 5
1
2
3
4
5
Sum is :: 15
Product is :: 120
5.
import java.util.Scanner;
class Q5
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the sixe of array :: ");
int l = sc.nextInt();
int[] arr = new int[l];
for(int i=0;i<l; i++)
{
arr[i] = sc.nextInt();
}
int max = arr[0];
int min = arr[0];
for(int i=1;i<arr.length;i++)
{
if(arr[i]>max)
{
max = arr[i];
}
if(arr[i]<min)
{
min = arr[i];
}
}
System.out.println("Maximum is :: " + max);
System.out.println("Minimum is :: " + min);
}
}
Output:-
Enter the sixe of array :: 5
10
50
30
40
20
Maximum is :: 50
Minimum is :: 10
6.
class Q6
{
public static void main(String args[])
{
int arr[]={50, 10, 20, 40, 30};
int temp;
for(int i=0;i<5;i++)
{
for(int j=i + 1;j<5; j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println("Sorted Array is :: ");
for(int i=0;i<5;i++)
{
System.out.println(arr[i]);
}
}
}
Output:-
Sorted Array is ::
10
20
30
40
50
7.
import java.util.Scanner;
class Q7
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a 3 digit number: ");
int n=sc.nextInt();
int sum = 0;
while (n!=0)
{
int d= n%10;
sum += d;
n /= 10;
}
System.out.println("Sum is :: " + sum);
}
}
Output:-
Enter a 3 digit number: 456
Sum is :: 15
8.
import java.util.Scanner;
class Q8
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
switch(n%2)
{
case 0:
System.out.println("Number is even.");
break;
case 1:
System.out.println("Number is odd.");
break;
}
}
}
Output:-
Enter a number: 10
Number is even.
9.
import java.util.Scanner;
class Q9
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter first value :: ");
int a = sc.nextInt();
System.out.print("Enter second value :: ");
int b = sc.nextInt();
switch (a>b ? 1 : b > a ? 2 : 0)
{
case 1:
System.out.println(a+" is greater than "+b);
break;
case 2:
System.out.println(b+" is greater than "+a);
break;
default:
System.out.println("Both numbers are equal.");
}
}
}
Output:-
Enter first value :: 100
Enter second value :: 200
200 is greater than 100
10.
import java.util.Scanner;
class Person
{
int age;
String name, address;
Person(String name,int age,String address)
{
this.name=name;
this.age=age;
this.address=address;
}
void setname(String name)
{
this.name=name;
}
String getname()
{
return name;
}
void setage(int age)
{
this.age=age;
}
int getage()
{
return age;
}
void setaddress(String address)
{
this.address=address;
}
String getadd()
{
return address;
}
void introduce()
{
System.out.println("Name :: " + name + "\nAge :: " + age +
"\nAddress :: " + address);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String name, address;
int age;
System.out.print("Enter your name :: ");
name = sc.next();
System.out.print("Enter your age :: ");
age = sc.nextInt();
System.out.print("Enter your address :: ");
address = sc.next();
Person obj = new Person(name,age,address);
obj.introduce();
}
}
Output:-
Enter your name :: Naimish
Enter your age :: 18
Enter your address :: Anjar
Name :: Naimish
Age :: 18
Address :: Anjar
11.
import java.util.Scanner;
class TemperatureConverter
{
double fahrenheitToCelsius(double fahrenheit)
{
return (fahrenheit - 32) * 5 / 9;
}
double celsiusToFahrenheit(double celsius)
{
return celsius * 9 / 5 + 32;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
TemperatureConverter c = new TemperatureConverter();
System.out.print("Enter a temperature in Fahrenheit :: ");
double fahrenheit = c.fahrenheitToCelsius(sc.nextDouble());
System.out.println(fahrenheit+"C");
System.out.print("Enter a temperature in Celsius :: ");
double celsius = c.celsiusToFahrenheit(sc.nextDouble());
System.out.println(celsius+"F");
}
}
Output:-
Enter a temperature in Fahrenheit :: 212
100.0C
Enter a temperature in Celsius :: 100
212.0F
12.
import java.util.Scanner;
class Q12
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String name,color;
int age;
System.out.print("Enter Name : ");
name = sc.next();
System.out.print("Enter Age : ");
age = sc.nextInt();
System.out.print("Enter your Favourite Color : ");
color= sc.next();
String s1=String.format("Name is %s.",name);
String s2=String.format("Age is %s.",age);
String s3=String.format("Favourite Color is %s.",color);
System.out.println("_______User information_______");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println("_______ Using Wrapper Class _______");
int num = 20;
Integer x = new Integer(num);
System.out.println("Wrapping value : " + x);
String n = "20";
Integer y = new Integer(n);
System.out.println("Parsed value : " + y);
System.out.println( "Addition of X and Y : " + (x + y));
}
}
Output:-
Enter Name : Naimish
Enter Age : 18
Enter your Favourite Color : Black
_______User information_______
Name is Naimish.
Age is 18.
Favourite Color is Black.
_______ Using Wrapper Class _______
Wrapping value : 20
Parsed value : 20
Addition of X and Y : 40
13.
import java.util.Scanner;
abstract class Marks
{
abstract float getPercentage();
}
class A extends Marks
{
float s1,s2,s3;
A(float s1, float s2, float s3)
{
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
}
float getPercentage()
{
float total = s1 + s2 + s3;
return (total / 300) * 100;
}
}
class B extends Marks
{
float s1, s2, s3, s4;
B(float s1, float s2, float s3, float s4)
{
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
this.s4 = s4;
}
float getPercentage()
{
float total = s1 + s2 + s3 + s4;
return (total / 400) * 100;
}
}
public class Result
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter marks of 3 subjects :: ");
A a = new A(sc.nextFloat(),sc.nextFloat(),sc.nextFloat());
System.out.println("Enter marks of 4 subjects :: ");
B b = new B(sc.nextFloat(),sc.nextFloat(),sc.nextFloat(),sc.nextFloat());
System.out.println("Percentage of student A :: " + a.getPercentage());
System.out.println("Percentage of student B :: " + b.getPercentage());
}
}
Output:-
Enter marks of 3 subjects ::
80
80
90
Enter marks of 4 subjects ::
80
80
90
90
Percentage of student A :: 83.33333
Percentage of student B :: 85.0
14.
class Base
{
public Base()
{
System.out.println("Base");
}
}
class Derived extends Base
{
public Derived()
{
System.out.println("Derived");
}
}
class DeriDerived extends Derived
{
public DeriDerived()
{
System.out.println("DeriDerived");
}
}
public class Test
{
public static void main(String[] args)
{
Derived b = new DeriDerived();
}
}
Output:-
Base
Derived
DeriDerived
In these code there are no errors.
Here we make an object of a class.we know that it is a rule in java that when we make an
object of a class the default constructor gets automatically called.So first "BASE" class
constructor is called .Then "DERIVED" class constructor is called . Then "DERIDERIVED"
class constructor is called.
15.
class Base
{
public Base()
{
System.out.print("Base ");
}
public Base(String s)
{
System.out.print("Base: " + s);
}
}
class Derived extends Base
{
public Derived(String s)
{
/*super(); // Stmt-1*/
super(s); // Stmt-2
System.out.print("Derived ");
}
}
class Test2
{
public static void main(String[] args)
{
Base base = new Derived("Hello ");
}
}
output :-
Base: Hello Derived
The error is in Stmt-1 where the super() method is called with no arguments. Since class
"BASE" has a parameterized constructor, it is mandatory to call the super constructor
with a parameter.
To solve the error you have to remove either one statement.
16.
abstract class Car
{
static
{
System.out.print("1");
}
public Car(String name)
{
super();
System.out.print("2");
}
{
System.out.print("3");
}
}
public class BlueCar extends Car
{
{
System.out.print("4");
}
public BlueCar()
{
super("blue");
System.out.print("5");
}
public static void main(String[] gears)
{
new BlueCar();
}
}
Output:-13245
Here first static block is called(1).
Then the default argument is called(3).
Then the constructor car is called(2).
Then the default argument of class bluecar is called(4).
then the constructor bluecar is called(5).
so here we can say that java first executes the static block,
then default arguments,and then constructors.
17.
class Math
{
public final double secret = 2;
}
class ComplexMath extends Math
{
public final double secret = 4;
}
public class InfiniteMath extends ComplexMath
{
public final double secret = 8;
public static void main(String[] numbers)
{
Math math = new InfiniteMath();
System.out.print(math.secret);
}
}
Output:-
2.0
Here method overriding is performed and secret is a non static variable so it's
value can be changed.
Here the output is 2.0 because we make an object of class "MATH".
If we make an object of class "COMPLEXMATH" then the output will be 4.0
18.
public class Test3
{
public void print(Integer i)
{
System.out.println("Integer");
}
public void print(int i)
{
System.out.println("int");
}
public void print(long i)
{
System.out.println("long");
}
public static void main(String args[])
{
Test3 test = new Test3();
test.print(10);
}
}
Output:-
int
Here we can say that JVM will first see the nearest value of the datatype.
Here Integer is an object and wrapper class.
Int is an primitive datatype.