Question

How to arrange the result neatly?

public class LabTest {
public static void main(String[] args) {
String[] state= {"Johor", "Kedah","Kelantan","Melaka","Negeri Sembilan","Pahang","Perak","Perlis","Pulau Pinang","Sabah","Sarawak","Selangor","Terengganu","Wilayah Persekutuan Labuan","Wilayah Persekutuan Kuala Lumpur"};
int [] rainfall= {1133,1312,1699,1220,1450,1596,1350,1189,1347,1987,1999,1125,1789,1980,1374};
int temp;
String tempN;
for (int i=0;i<rainfall.length;i++){
for (int j=0;j<rainfall.length;j++){
if (rainfall[i]>rainfall[j]){
temp=rainfall[i];
rainfall[i]=rainfall[j];
rainfall[j]=temp;
  
tempN=state[i];
state[i]=state[j];
state[j]=tempN;
}
}
}

for (int i=0;i<rainfall.length;i++){
System.out.print(state[i] + "\t");
}
System.out.println("");
for (int i=0;i<rainfall.length;i++){
System.out.print(rainfall[i] + "\t");   
}
}
}

Sabah Kelantan Pahang Perak Pulau Pinang Kedah Sarawak Wilayah Persekutuan Labuan Terengganu Negeri Sembilan Wilayah Persekut

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#source code:

public class LabTest {
public static void main(String[] args) {
String[] state= {"Johor", "Kedah","Kelantan","Melaka","Negeri Sembilan","Pahang","Perak","Perlis","Pulau Pinang","Sabah","Sarawak","Selangor","Terengganu","Wilayah Persekutuan Labuan","Wilayah Persekutuan Kuala Lumpur"};
int [] rainfall= {1133,1312,1699,1220,1450,1596,1350,1189,1347,1987,1999,1125,1789,1980,1374};
int temp;
String tempN;
for (int i=0;i<rainfall.length;i++){
for (int j=0;j<rainfall.length;j++){
if (rainfall[i]>rainfall[j]){
temp=rainfall[i];
rainfall[i]=rainfall[j];
rainfall[j]=temp;
  
tempN=state[i];
state[i]=state[j];
state[j]=tempN;
}
}
}

for (int i=0;i<rainfall.length;i++){
System.out.printf("%-30.30s %-30.30s%n", state[i],rainfall[i]);
System.out.println("");
}

}
}

#output:

user@user-Latitude - 3490:-/Desktops javac LabTest.java user@user-Latitude-3490:-/Desktops java LabTest Sarawak 1999 Sabah 19

#if you want display horizontal way just comment below...if you like give thumbs up....

Add a comment
Know the answer?
Add Answer to:
How to arrange the result neatly? public class LabTest { public static void main(String[] args) {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {...

    Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {         int[] array = {7, 1, 3, 2, 42, 76, 9};         insertionSort(array);     }     public static int[] insertionSort(int[] input) {         int temp;         for (int i = 1; i < input.length; i++) {             for (int j = i; j > 0; j--) {                 if (input[j] < input[j - 1]) {                     temp = input[j];                     input[j] = input[j - 1];                     input[j - 1] = temp;                 }             }             display(input, i);...

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • What is the Java output? Part One: class Driver { public static void main(String[] args) {...

    What is the Java output? Part One: class Driver { public static void main(String[] args) { int a = 5; int b = 3; if (a < b || a * 2 < b) System.out.print(a - b); System.out.print(b + a); } } Part Two: class Driver { public static void main(String[] args) { int a = 5; int b = 8; if (a < b) if (a * 2 < b) System.out.print("foo"); else System.out.print("bar"); else System.out.print("buz"); } }

  • package array; public class Test { static int[] data = (0,1,2,3,4,5,6,7,8); public static void main (String[]...

    package array; public class Test { static int[] data = (0,1,2,3,4,5,6,7,8); public static void main (String[] a) { for ( int i = 0;i<data.length; i++) { if(i %3 == 0) { System.out.print("A"); System.out.print(data[i]); System.out.print(" "); } } I need an explanation of what this code is doing ? is there anything wrong with it }}

  • c) public class Test { public static void main(String[] args) { T t1 = new T();...

    c) public class Test { public static void main(String[] args) { T t1 = new T(); T t2 = new T(); System.out.println("t1's i = " + t1.i + " and j = " + t1.j); System.out.println("t2's i = " + t2.i + " and j = " + t2.j); } } class T { static int i = 0; int j = 0; T() { i++; j = 1; } } Why is  t1's i = 2 ? It should be...

  • Review the following code: public class Looping {    public static void main(String[] args) {      ...

    Review the following code: public class Looping {    public static void main(String[] args) {       for (int i = 1; i <= 5; i++) {          for (int j = 1; j <= 5; j++) {             System.out.println(i + " x " + j + " = " + (i * j));          }       }    } } What is the output from the code above? Replace this text with your solution What happens if you change the...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println () 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...

  • What is printed by running the following code? public static void main(String[] args) { int[] nums...

    What is printed by running the following code? public static void main(String[] args) { int[] nums = {2, 3, 4}; int n = 5; changeMe1(n, nums); System.out.print( n ); System.out.print(nums[0]); changeMe2(n, nums); System.out.print( n ); System.out.print(nums[0]); } public static void changeMe1(int number, int[] list) { number++; list[0]++; } public static void changeMe2(int number, int[] list) { number = 9; list = new int[1]; list[0] = 99; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT