Sie sind auf Seite 1von 3

Doubt: 1

Change the user input to integer


Isn't it a very tedious job to enter the values each time before the compilation in the method
itself. Now if we want to enter an integer value after the compilation of a program and force the
JVM to ask for an input, then we should use Integer.parseInt(string str).

As we know that JVM reads each data type in a String format. Now consider a case where we to
enter a value in int primitive type, then we have to use int x = Integer.parseInt("any String") ,
because the value we have entered is in integer but it will be interpreted as a String so, we have
to change the string into integer. Here Integer is a name of a class in java.lang package and
parseInt() is a method of Integer class which converts String to integer. If we want to enter a
integer in a method or class using keyboard, then we have to use a method parseInt().

In this program we are going to calculate a area of a rectangle by using two classes named
Rectangle and EnterValuesFromKeyboard. In the first class we have used two methods, show(int
x, int y) and calculate(). First method show() is taking two variables as input and second method
calculate() calculates the area of a rectangle. In the second class which is also our main class we
declare a will declare our main method. Inside this method we will create a object of a Rectangle
class. Now we ask the user to input two values and stored those values in the variables. These
entered values will be changed into integer by using parseInt() method. Now these variables are
passed in the method show() and the area will be calculated by calculate() method. These
methods will be called by the instance of Rectangle class because it is the method of Rectangle
class.

Here is the code of the program

class Rectangle{
int length, breadth;
void show(int x, int y){
length = x;
breadth = y;
}
int calculate(){
return(length * breadth);
}
}
public class EnterValuesFromKeyboard{
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
rectangle.show(a, b);
System.out.println(" you have entered these values : " + a + " and " +
b);
int area = rectangle.calculate();
System.out.println(" area of a rectange is : " + area);
}
}

Output of the program is given below:

C:\java>java
EnterValuesFromKeyboard 4 5
you have entered these values : 4
and 5
area of a rectange is : 20

Doubt: 2

Find out the prime number

This lesson of Java programming language will teach you the coding to find out whether a given
number is prime or not. Here we have used the 'for loop' statement and given the required
condition for a prime number. As we know, a prime number is only divided by 1 and itself, in
other words it has no other factorial other than 1 and the number itself.

Here, first make a class and named as "Primenumber" and take an integer as num=11, and define
an integer 'i' as the integer other than 1 and the given number. That means, i>2 and i<num. Now
apply this in the "for loop" statement and define an integer n=num/i as given below in the
example. Now apply the "if" condition and if the reminder of the earlier equation comes "0",
then the result will be not prime. Again the loop system will check the above condition until it
has not satisfied from the starting point(2) to the end(10). Here under this loop we have to use
the "break" statement for unnecessary checking further one point where the reminder comes
zero(0).

Now after checking the whole condition, if the reminders does not come "zero", then we have to
again apply the "if" condition and check whether i=num or not. If it is true then number (num) is
prime. As we have taken here as num=11, then after compiling and running the program, the
result will show that num is prime number.

Here is the code program:

class Prime_number {
public static void main(String[] args) {
int num = 11;
int i;
for (i=2; i < num ;i++ ){
int n = num%i;
if (n==0){
System.out.println("not Prime!");
break;
}
}
if(i == num){
System.out.println("Prime number!");
}
}
}

Factorial logic

while (temp > 0)


{
factorial *= temp;
temp--;
}

Das könnte Ihnen auch gefallen