import java.text.Collator; import java.util.Scanner; public class SortStudent {
ID: 3656290 • Letter: I
Question
import java.text.Collator; import java.util.Scanner; public class SortStudent { public static void main (String[] args){ int numOfStu; Students[] student; //add your code here Scanner kb = new Scanner(System.in); System.out.println("Enter the number of students:"); numOfStu = kb.nextInt(); student = new Students [numOfStu]; System.out.println("*Sort by name:"); bubbleSort(student, 1); //output sorted array for( int i = 0; i < student.length; i++){ System.out.println(student[i].getStudname() + " " + student[i].getID()); } System.out.println("*Sort by id:"); bubbleSort(student, 0); //output sorted array for( int j=0; j< student.length; j++){ System.out.println(student[j].getStudname() + " " + student[j].getID()); } } public static void bubbleSort(Students[] arr, int sortType){ boolean swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; j++; //Compare the first and the second and so on... for (int i = 0; i < arr.length - j; i++) { if ( arr[i] > arr[i+1] ) { // if the first num is > the second num it swaps them //swap arr[i] and arr[i+1] //write your code tmp = arr[i+1]; arr[i+1] = arr[i]; arr[i] = tmp; swapped = true ; } } } } } import java.util.Scanner; public class Students { private String Studname; private int ID; Scanner kb = new Scanner(System.in); public void readInput(){ System.out.print("Enter student's name and id number:"); Studname = kb.next(); ID = kb.nextInt(); System.out.println(); } public String getStudname() { return Studname; } public int getID() { return ID; } } it is giving me a null exception at line 16 when i try to set the students names and IDExplanation / Answer
// Program calculates factorials. import javax.swing.*; public class Factorial { // main method begins execution of Java program public static void main( String args[] ) { JTextArea outputArea = new JTextArea( 5, 10 ); String outputString = “X X! ”; for ( int number = 1; numberint factorial = 1; for ( int smaller = 1; smallerfactorial *= smaller; outputString += “ ” + number + “ ” + factorial; } outputArea.setText( outputString ); JOptionPane.showMessageDialog( null, outputArea, “Factorial”, JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } } // end class Factorial
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.