0
Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts
Example:
public class pyramid {
public static void main(String[] args) {
int spacecounter=4,starcounter=1;
for(int i=1;i<=5;i++)
{
System.out.println();
for(int j=1;j<=spacecounter;j++)
{
System.out.print(" ");
}
for(int k=1;k<=starcounter;k++)
{
System.out.print("*");
}
spacecounter--;
starcounter=starcounter+2;
}
}
}
Output:
*
***
*****
*******
*********
Example:
package largestnumber;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Scanner;
import org.apache.commons.collections.IteratorUtils;
public class highestnumber {
public static void main(String[] args) {
int i = 0;
ArrayList<Integer> arraylist = new ArrayList<>();
Scanner s = new Scanner(System.in);
System.out.println("Enter size of array:");
int n = s.nextInt();
System.out.println("You have to enter " + (n - i) + " values:");
for (; i < n; i++)
{
{
arraylist.add(s.nextInt());
}
Collections.sort(arraylist);
System.out.println("All Elements in the Array List are: " +arraylist);
System.out.println("Lowest Number in the Array is:"
+ arraylist.get(0));
System.out.println("Second Largest Number in the Array is: "
+ arraylist.get(arraylist.size() - 2));
System.out.println("Largest Number in the Array is: "
+ arraylist.get(arraylist.size() - 1));
}
}
Output:
Enter size of array:
5
You have to enter 5 values:
3452
7865
9783
1748
9934
All Elements in the Array List are: [1748, 3452, 7865, 9783, 9934]
Lowest Number in the Array is:1748
Second Largest Number in the Array is: 9783
Largest Number in the Array is: 9934
Prime Number or Not using Java
A prime number is a naturalnumber greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number.Ex: String s = "This is $100 Dollars % * ^";
- To extract '$' from the string can be done using regular expressions.
- To extract '100' from the String .
- Only to get characters.
- Excluding the spacing in the above string.
Wrapper Class:
In wrapper class we have Objects which stores primitive data type or which wraps primitive data types.
Advantages:
- We cannot pass Primitive data type data between two application over the Internet.So Wrapper class needed in that case.
- When we send data to server then the server expects data to be in object format rather than primitive type of data.
- Objects in wrapper class can hold primitive data types.
Syntax:
Integer i = new Integer(123);
Float f = new Float(45f);
same for the remaining wrapper classes.
Primitive Data Types
|
Wrapper Classes
|
int
|
Integer
|
byte
|
Byte
|
char
|
Character
|
short
|
Short
|
long
|
Long
|
float
|
Float
|
double
|
Double
|
boolean
|
Boolean
|
Wrapper classes have boxing and unboxing.
Boxing:
boxing means converting primitive data type to objects is boxing.
Ex:
int i=10; // primitive type
Integer obj=new Integer(i); //coverting to objects
System.out.println(obj);
Output:
10
UnBoxing:
unboxing means converting objects to corresponding primitive data type is unboxing.
Ex:
int i=10; // primitive type
Integer obj=new Integer(i); //coverting to objects
int j = obj.intValue(); //converting to primitive type:unboxing
10
Subscribe to:
Posts (Atom)


















