Java Basic Problems

Java: RunTime Input from User Example

I have discussed about how to take input from user in runtime from command line here.

Here I will try to give a simple example regarding this.

In this example, our program will take total student number and total subject as input, then it will take input of each subject number. After calculating the average number of total subject, if any student gets less than 40 it will show that student is failed and if any student gets more than 40 that student is passed.

Here is the given Code:

import java.io.*;

class student

{

public static void main(String args[])

{

String inputa;

int num_std;        // Total Number of Student

int sub_std;        // Total Subject of Student

System.out.println(“************************************”);

System.out.println(“\n”);

System.out.println(“Enter the Total Number of Students:”);

System.out.println(“\n”);

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

try{

inputa=br.readLine();

num_std=Integer.parseInt(inputa);

System.out.println(“Enter the Total Subject of Student:”);

System.out.println(“\n”);

inputa=br.readLine();

sub_std= Integer.parseInt(inputa);

int i=0;

int j=0;

int z=0;

System.out.println(“\n”);

for (i=1;i<=num_std;i++)

{

System.out.println(“Enter Student  :” +i+” :Subject Number:”);

System.out.println(“\n”);

for (j=1;j<=sub_std;j++)

{

inputa= br.readLine();

z= z+Integer.parseInt(inputa);

if(j==sub_std)

{

System.out.println(“Student:”+i+”Total Number:”+z);

int om=(z)/sub_std;

if(om<40)

{

System.out.println(“Student:”+i+”fails”);

System.out.println(“Student  :”+i+” :  Average Number:”+om);

om=0;

z=0;

}

else

{

System.out.println(“Student  :”+i+” :  passed”);

System.out.println(“Student  :”+i+” :  Average Number:”+om);

om=0;

z=0;

}

}

} //End of student  subject loop

} //End of student num loop

System.out.println(“\n”);

System.out.println(“************************************”);

}

catch(IOException e)

{}

}

}

OUTPUT:

Java Basic Problems

Java:What is Static Variable?

What is Static Variable:
Static variable is that variable which has single copy in memory and is shared by all objects, so if there is any modifications to that static variable will also modify it’s value in all objects.

 

Important Notes about Static Variable:

** Fields that have the static modifier in their declaration are called static fields or class variables.

** Class variables (Static Variables) are referenced by the class name itself, as in

test1.demo //Code is given below

** Static Variables are associated with the class, rather than with any object. So, every instance of the class shares a class variable /Static Variable, which is in one fixed location in memory. Any object can change the value of a class variable.

static variable java

Code:
public class staticVariable

{

static int demo=0;

staticVariable()

{

demo++;

}

public static void main(String[] args)

{

staticVariable test1=new staticVariable();

System.out.println(“Value of demo in test1:”+test1.demo);

System.out.println(“***********************************” );

staticVariable test2=new staticVariable();

System.out.println(“Value of demo in test1:”+test1.demo);

System.out.println(“Value of demo in test2:”+test2.demo);

System.out.println();

}

}

 

OUTPUT
 
 
 
 

 

static variable java