import java.util.Arrays; public class MedianSort { static int count; static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static int compareAndSwap(int a[], int i, int j) { int count = 1; if (a[i] > a[j]) { swap(a, i, j); } return count; } static int median(int a[], int i, int j) { int count = 0; if (i >= j) { return 0; } if (j - i >= 2) { int k = (i + j) / 2; count += compareAndSwap(a, i, k); count += compareAndSwap(a, i, j); count += compareAndSwap(a, k, j); } return count; } static int medianSort(int a[], int i, int j) { int count = 0; if ( j -i <= 1) { return 0; } // if more than 2 elements, than using median sort if (j - i >= 2) { // get the median of three count += median(a, i, j); // partition using median as pivot int k = (i + j) / 2; int pivot = a[k]; int left = i + 1; int right = j - 1; while (left < right) { while (a[left] < pivot) { count++; left++; } count++; while (a[right] > pivot) { count++; right--; } count++; if (left < right) { swap(a, left, right); left++; right--; } } if (left == right) { count += medianSort(a, i, left - 1); count += medianSort(a, right + 1, j); } else { count += medianSort(a, i, left); count += medianSort(a, right, j); } } return count; } public static void main(String[] args) { int[] a = { 2, 5, 3, 6, 7, 1, 10, 4, 0, 9, 5 }; int count = medianSort(a, 0, a.length - 1); System.out.println(Arrays.toString(a)); System.out.println(count); } }
import java.util.Scanner; public class heapsort { private static int N; public static void sort(int arr[]) { heapify(arr); for (int i = N; i > 0; i--) { swap(arr,0, i); N = N-1; maxheap(arr, 0); } } public static void heapify(int arr[]) { N = arr.length-1; for (int i = N/2; i >= 0; i--) maxheap(arr, i); } public static void maxheap(int arr[], int i) { int left = 2*i; int right = 2*i + 1; int max = i; if (left <= N && arr[left] > arr[i]) max = left; if (right <= N && arr[right] > arr[max]) max = right; if (max != i) { swap(arr, i, max); maxheap(arr, max); } } public static void swap(int arr[], int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } public static void main(String[] args) { Scanner scan = new Scanner( System.in ); System.out.println("Heap Sort Test\n"); int n, i; System.out.println("Enter number of integer elements"); n = scan.nextInt(); int arr[] = new int[ n ]; System.out.println("\nEnter "+ n +" integer elements"); for (i = 0; i < n; i++) arr[i] = scan.nextInt(); sort(arr); System.out.println("\nElements after sorting "); for (i = 0; i < n; i++) System.out.print(arr[i]+" "); System.out.println(); } }
package homespecification; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ImageIcon; import javax.swing.JFrame; /** * * @author Anucha Famila * */ public class kitchen extends javax.swing.JFrame { private ImageIcon K1 = new ImageIcon("K1.jpg"); private ImageIcon K2 = new ImageIcon("K2.jpg"); //private ImageIcon private pictureJLabel IBIImage = new pictureJLabel(); private ItemHandler handler = new ItemHandler(); private JFrame frame = new JFrame(); /** * Creates new form kitchen */ public kitchen() { initComponents(); kitchenJComboBox.addItemListener(handler); frame.add(IBIImage); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); refrigeratorjComboBox1 = new javax.swing.JComboBox<>(); kitchenJComboBox = new javax.swing.JComboBox<>(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jTextField3 = new javax.swing.JTextField(); jLabel6 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jComboBox1 = new javax.swing.JComboBox<>(); jLabel9 = new javax.swing.JLabel(); pictureJLabel = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel1.setText("ห้องครัว"); jLabel2.setText("ตู้เย็น"); refrigeratorjComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "LG ตู้เย็น InstaView Door-in-Door 55,998 บาท", "MITSUBISHI ELECTRIC ตู้เย็น 2 ประตู 7,990 บาท", "TOSHIBA ตู้เย็น 1 ประตู (5.2 คิว,สีเทา) ราคา 4,990", " ", " " })); kitchenJComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "ชุดเครื่องครัว thaimodernkitchen SDS-1 54600 บาท", "ชุดเครื่องครัว thaimodernkitchen SDS-2 90000 บาท", "ชุดเครื่องครัว thaimodernkitchen SDS-3 86900 บาท" })); kitchenJComboBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { kitchenJComboBoxActionPerformed(evt); } }); jLabel3.setText("ชุดห้องครัว"); jButton1.setText("ตกลง"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel6.setText("ราคาสินค้าทั้งหมด"); jLabel5.setText("รายละเอียด"); jLabel7.setText("รายละเอียด"); jLabel8.setText("เครื่องกรองน้ำ"); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "eSpring ของ Amway 29,970 บาท", "Coway คอร์ CHP-671R (Core) R.O. System 46,800.00 บาท", "PURE DM01 UV 8,990 บาท", " " })); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); jLabel9.setText("รายละเอียด"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel4) .addGap(103, 103, 103)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(29, 29, 29) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(43, 43, 43) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(layout.createSequentialGroup() .addComponent(jLabel6) .addGap(18, 18, 18) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(63, 63, 63) .addComponent(jButton1)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3) .addComponent(jLabel2) .addComponent(jLabel8)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(refrigeratorjComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(kitchenJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel7) .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)) .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)) .addGap(56, 56, 56) .addComponent(pictureJLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(146, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(30, 30, 30) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(kitchenJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7)) .addComponent(pictureJLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(refrigeratorjComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5)) .addGap(21, 21, 21) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel8) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel9)) .addGap(51, 51, 51) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6) .addComponent(jButton1))) .addGroup(layout.createSequentialGroup() .addGap(18, 18, 18) .addComponent(jLabel4))) .addContainerGap(30, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void kitchenJComboBoxActionPerformed(java.awt.event.ActionEvent evt) { // kitchenJComboBox.getValue("");// TODO add your handling code here: } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see https://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(kitchen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(kitchen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(kitchen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(kitchen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new kitchen().setVisible(true); } }); } private class ItemHandler implements ItemListener { @Override public void itemStateChanged(ItemEvent event){ if(event.getSource()==kitchenJComboBox) if(kitchenJComboBox.getSelectedItem().equals("LG ตู้เย็น InstaView Door-in-Door 55,998 บาท")){ IBIImage.setIcon(K1); }else if (kitchenJComboBox.getSelectedItem().equals("MITSUBISHI ELECTRIC ตู้เย็น 2 ประตู 7,990 บาท")){ IBIImage.setIcon(K2); } } } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JComboBox<String> jComboBox1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JTextField jTextField3; private javax.swing.JComboBox<String> kitchenJComboBox; private javax.swing.JLabel pictureJLabel; private javax.swing.JComboBox<String> refrigeratorjComboBox1; // End of variables declaration }
package zadanie51; //Zadanie5.1 import java.io.*; class pole_prostokata { double a, b, pole; public void czytaj_dane() // deklaracja i opis metody czytaj_dane throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Program oblicza pole prostokata."); System.out.println("Podaj bok a."); a = Double.parseDouble(br.readLine()); System.out.println("Podaj bok b."); b = Double.parseDouble(br.readLine()); } public void przetworz_dane() //deklaracja i opis metody przetwoz_dane { pole = a*b; } public void wyswietl_wynik() // deklaracja i opis metody wyswietl_wynik { System.out.print("Pole prostokata o boku a = "); System.out.printf("%2.2f", a); System.out.print("i boku b = "); System.out.printf("%2.2f", b); System.out.print(" wynosi "); System.out.printf("%2.2f.\n", pole); } } public class Main { public static void main(String[] args) throws IOException { pole_prostokata pole = new pole_prostokata(); //deklaracja zmiennej, utworzenie obiektu i przypisanie go do zmiennej pole.czytaj_dane(); //wywoanie metody czytaj_dane() pole.przetworz_dane(); //wywoanie metody przetworz_dane() pole.wyswietl_wynik(); //wywoanie metody wyswietl_wynik() } }
import java.io.*; import java.util.Scanner; import java.util.InputMismatchException; public class SwapNumbers{ public static void main(String[] args) { int x,y; int temp; Scanner sc = new Scanner(System.in); x = sc.nextInt(); y = sc.nextInt(); System.out.println("Before Swapping\nx = "+x+"\ny = "+y); temp = x; x = y; y = temp; System.out.println("The values after swapping are :\nx = "+x+" \ny = "+y); } }
import java.util.Scanner; import java.lang.Math; public class HelloWorld{ public static void main(String []args){ int[][] arr = new int[][] { {3, 4, 1, 8}, {1, 4, 9, 11}, {76, 34, 21, 1}, {2, 1, 4, 5} }; // Calling the function //int[] rowMaxValue = maxelement(4, arr); //printArray(rowMaxValue); int[] rowMinValue = minelement(4, arr); printArray(rowMinValue); int maxRowEle = findMaxEle(rowMinValue); System.out.println("Row Max Ele: "+maxRowEle); int[] colMaxValue = largestInColumn(4, arr); printArray(colMaxValue); int colMinEle = findMinEle(colMaxValue); System.out.println("Col Min Ele: "+colMinEle); } public static int findMaxEle(int[] a){ int max = a[0]; for(int i = 1; i < a.length;i++) { if(a[i] > max) { max = a[i]; } } return max; } public static int findMinEle(int[] a){ int min = a[0]; for(int i = 1; i < a.length;i++) { if(a[i] < min) { min = a[i]; } } return min; } public static int[] largestInColumn(int cols, int[][] arr) { int[] colMaxValue = new int[cols]; for (int i = 0; i < cols; i++) { // Initialize max to 0 at begining // of finding max element of each column int maxm = arr[0][i]; for (int j = 1; j < arr[i].length; j++) if (arr[j][i] > maxm) maxm = arr[j][i]; colMaxValue[i] =maxm; //System.out.println(maxm); } return colMaxValue; } public static int[] maxelement(int no_of_rows, int[][] arr) { int i = 0; // Initialize max to 0 at begining // of finding max element of each row int max = 0; int[] result = new int[no_of_rows]; while (i < no_of_rows) { for (int j = 0; j < arr[i].length; j++) { if (arr[i][j] > max) { max = arr[i][j]; } } result[i] = max; max =0; i++; } //printArray(result); return result; } public static int[] minelement(int no_of_rows, int[][] arr) { int i = 0; // Initialize max to 0 at begining // of finding max element of each row int[] result = new int[no_of_rows]; while (i < no_of_rows) { int max = arr[i][0]; for (int j = 0; j < arr[i].length; j++) { if (arr[i][j] < max) { max = arr[i][j]; } } result[i] = max; //max =0; i++; } //printArray(result); return result; } // Print array element private static void printArray(int[] result) { for (int i =0; i<result.length;i++) { System.out.println(result[i]); } } }
import java.util.Scanner; public class Lottery { static int guess[] = new int[6],actual[]= new int[6]; private static void aquireGuess() { Scanner keyboard = new Scanner(System.in); System.out.println("Time to play the lottery please choose three numbers ranging from zero to nine."); System.out.print("Please enter first number:"); guess[0] = keyboard.nextInt(); System.out.print("Please enter second number:"); guess[1] = keyboard.nextInt(); System.out.print("Please enter third number:"); guess[2] = keyboard.nextInt(); System.out.print("Please enter first number:"); guess[0] = keyboard.nextInt(); System.out.print("Please enter first number:"); guess[0] = keyboard.nextInt(); System.out.print("Please enter first number:"); guess[0] = keyboard.nextInt(); keyboard.close(); } private static void generateLottery() { actual[0] = (int)(Math.random()*10); actual[1] = (int)(Math.random()*10); actual[2]= (int)(Math.random()*10); actual[0] = (int)(Math.random()*10); actual[0] = (int)(Math.random()*10); actual[0] = (int)(Math.random()*10); } private static void compare() { if(guess[0]==actual[0]&&guess[1]==actual[1]&&guess[2]==actual[2]&&guess[2]==actual[1]) { System.out.println("Congradulations three matches in exact order!!! You win $1,000,000."); return; } int count = 0; for(int i=0;i<=2;i++) { for(int x=0;x<=2;x++) { if (actual[i]==guess[x]) { count++; guess[x]=-1; break; } } } switch(count) { case 0:System.out.println("Sorry better luck next time."); break; case 1:System.out.println("Good job. One match. You win $10."); break; case 2:System.out.println("Awsome you made two matches. You win $100."); break; case 3:System.out.println("Woohoo you matched all three. You win $1,000.");break; } } public static void main(String args[]) { aquireGuess(); generateLottery(); System.out.println("Your guess were "+guess[0]+" "+guess[1]+" "+guess[2]); System.out.println("The lottery numbers were "+actual[0]+" "+actual[1]+" "+actual[2]); compare(); } }
//Fig. 2.7 Addition.java //Addition program that displays ths sum of two numbers. import java.util.Scanner; //program uses class Scanner public class Addition{ //main method begins execution of Java application public static void main(String[]args){ //create a Scanner to obtain input from the command window Scanner input = new Scanner(System.in); int number1; //first number to Addition int number2; //second number to Addition int sum; //sum of number1 and number2 System.out.print("Enter first integer: "); //prompt number1 = input.nextInt(); //read first number from user System.out.print("Enter second integer: "); //prompt number2 = input.nextInt(); //read second number from user sum = number1 + number2; //add numbers, then store total in sum System.out.printf("Sum is %d\n", sum); }//end method main }//end class Addition
public class HelloWorld{ public static void main(String []args){ System.out.println("Hello World"); } }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more