What's new

Guys! Pa help sa code?

chiper

Enthusiast
Joined
May 1, 2018
Posts
184
Reaction
10
Age
22
Convert the following code to a user-defined class by using instance variables and methods.

***this Program is to Sort an Array in Ascending Order


import java.util.Scanner;
public class JavaExample
{
public static void main(String[] args)
{
int count, temp;

Scanner scan = new Scanner(System.in);
System.out.print("Enter number of elements you want in the array: ");
count = scan.nextInt();

int num[] = new int[count];
System.out.println("Enter array elements:");
for (int i = 0; i < count; i++)
{
num = scan.nextInt();
}
scan.close();
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++) {
if (num > num[j])
{
temp = num;
num = num[j];
num[j] = temp;
}
}
}
System.out.print("Array Elements in Ascending Order: ");
for (int i = 0; i < count - 1; i++)
{
System.out.print(num + ", ");
}
System.out.print(num[count - 1]);
}
}
 
Top