Thursday, December 5, 2019

Exercise - 1 (Basics)

Exercise - 1 (Basics)

a). Write a JAVA program to display default value of all primitive data type of JAVA

import java.io.*;
class Primitivevalues
{
static boolean B1;
static byte T1;
static int I1;
static float F1;
static double D1;
static char C1;
public static void main(String args[])
{

System.out.println("static  variable  Default values for BYTE:"+T1);
System.out.println("static  variable  Default values for INT:"+I1);
System.out.println("static  variable  Default values for FLOAT:"+F1);
System.out.println("static  variable  Default values for DOUBLE:"+D1);
System.out.println("static  variable  Default values for CHAR:"+C1);
System.out.println("static  variable  Default values for BOOLEAN:"+B1);
}
}

b). Write a java program that display the roots of a quadratic equation ax2+bx=0. Calculate
the discriminate D and basing on value of D, describe the nature of root.



import java.io.*;
import static java.lang.Math.sqrt;
import java.util.*;
class Roots
{

  void m1(int a,int b,int c)
 {
  double value=(Math.pow(b,2.0)-4*a*c);
   double discriminant=(double)Math.sqrt(value);
  if(value>0)
  {
   System.out.println("Roots are real numbers");
   double root1=(discriminant-b)/(2*a);
      double root2=(-b-discriminant)/(2*a);
      System.out.println("FirstRoot"+root1+"\nSecond Root"+root2);
  }
  else if(value==0)
  {
   double root1=(discriminant-b)/(2*a);
   System.out.println("Polynomial has one Root"+root1);
  }
  if(value<0)
  {
   System.out.println("Roots are imaginary numbers");
   System.out.println((-b+"i"+"+"+(-1*value))+"/"+(2*a));
   System.out.println((-b+"i"+"-"+(-1*value))+"/"+(2*a));
  }


 }
 public static void main(String [] args)
  {
   int a,b,c;
   System.out.print("Enter a,b,c values:");
   Scanner br=new Scanner(System.in);
    a=br.nextInt();
    b=br.nextInt();
    c=br.nextInt();
    new Roots().m1(a,b,c);
  }
}

c). Five Bikers Compete in a race such that they drive at a constant speed which may or may
not be the same as the other. To qualify the race, the speed of a racer must be more than the
average speed of all 5 racers. Take as input the speed of each racer and print back the speed
of qualifying racers.

import java.io.*;
import java.util.*;
class Race
{
public static void main(String [] args)
{
int R[]=new int[20];
float avg;
int sum=0;
System.out.println("Enter Speed of 5 members");
Scanner sc=new Scanner(System.in);
for(int j=1;j<=5;j++)
{
R[j]=sc.nextInt();
sum+=R[j];
}
avg=sum/5;
for(int i=1;i<=5;i++)
{
if(avg<R[i])
{
System.out.println("QualifiedRaceris:"+i+"with value="+R[i]);
}
}
}
}


d) Write a case study on public static void main(250 words)

public static void main(String[] args)
Java main method is the entry point of any java program. Its syntax is always public static void main(String[] args). You can only change the name of String array argument, for example you can change args to myStringArgs.
Also String array argument can be written as String... args or String args[].

public

This is the access modifier of the main method. It has to be public so that java runtime can execute this method. Remember that if you make any method non-public then it’s not allowed to be executed by any program, there are some access restrictions applied. So it means that the main method has to be public. Let’s see what happens if we define the main method as non-public.

public class Test 
{
static void main(String[] args)
{
     System.out.println("Hello World");   
}
}

OUTPUT:-
$ javac Test.java
$ java Test
Error: Main method not found in class Test, please define the main method as:
   public static void main(String[] args)

static

When java runtime starts, there is no object of the class present. That’s why the main method has to be static so that JVM can load the class into memory and call the main method. If the main method won’t be static, JVM would not be able to call it because there is no object of the class is present. Let’s see what happens when we remove static from java main method.
public class Test {
public void main(String[] args){
            System.out.println("Hello World");   
}
}
OUTPUT:-
$ javac Test.java
$ java Test
Error: Main method is not static in class Test, please define the main method as:
   public static void main(String[] args)

void

Java programming mandates that every method provide the return type. Java main method doesn’t return anything, that’s why it’s return type is void. This has been done to keep things simple because once the main method is finished executing, java program terminates. So there is no point in returning anything, there is nothing that can be done for the returned object by JVM. If we try to return something from the main method, it will give compilation error as an unexpected return value. For example, if we have the main method like below.
public class Test {
public static void main(String[] args){    
return 0;
}
}
OUTPUT:-
$ javac Test.java
Test.java:5: error: incompatible types: unexpected return value
     return 0;          
 ^
1 error

main

This is the name of java main method. It’s fixed and when we start a java program, it looks for the main method. For example, if we have a class like below.
public class Test {
public static void mymain(String[] args)
            System.out.println("Hello World");
}
}
OUTPUT:-
$ javac Test.java
$ java Test
Error: Main method not found in class Test, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

String[] args

Java main method accepts a single argument of type String array. This is also called as java command line arguments. Let’s have a look at the example of using java command line arguments.
public class Test {
public static void main(String[] args){
for(String s : args){
            System.out.println(s);
    }  }}
Above is a simple program where we are printing the command line arguments. Let’s see how to pass command line arguments when executing above program.
OUTPUT:-
$ javac Test.java
$ java Test 1 2 3
1
2
3

No comments:

Post a Comment

JAVA PROGRAMMING ASSIGNMENT 2 QUESTIONS

SIR C R REDDY COLLEGE OF ENGINEERING, ELURU DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING JAVA PROGRAMMING ASSIGNMENT 2 QUESTIONS ...