Anda di halaman 1dari 7

// Merge Sort Code Starts import java.util.Scanner; import java.io.

*; public class mergeSort2{ public static void main(String a[]){ int temp; Scanner sc=new Scanner (System.in); System.out.println("Enter N ( how many numbers to be sorted)"); int n = sc.nextInt(); int [] list=new int[n]; System.out.println("Enter "+n+ " numbers 1 by 1"); for (int i=0 ; i<n; i++) { int number = sc.nextInt(); list[i]=number; } System.out.println("List before sorting \n"); for(int i = 0; i < list.length; i++) System.out.print( list[i]+" "); System.out.println(); mergeSort(list,0, list.length-1); System.out.print("List after sorting \n"); for(int i = 0; i < list.length; i++) System.out.print(list[i]+" "); System.out.println(); } public static void mergeSort(int list[],int low, int high){ if (low >= high) { return; } int middle = (low + high) / 2; mergeSort(list, low, middle); mergeSort(list, middle + 1, high); merge(list, low,middle,high); } private static void merge(int list[], int low, int middle, int high) { int IstList_end= middle;

int IIndList_start = middle + 1; int l=low; while ((l <= IstList_end) && (IIndList_start <= high)) { if (list[low] < list[IIndList_start]) { low++; } else { int temp = list[IIndList_start]; for (int j = IIndList_start-1; j >= low; j--) { list[j+1] = list[j]; } list[low] = temp; low++; IstList_end++; IIndList_start++; } } } } //Duplicate import java.io.*; public class deletedupe { public static void main(String[] args) throws IOException { InputStreamReader ir=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(ir); String s; System.out.println("Enter any sentence"); s=br.readLine(); int l=s.length(); String[] wDupes = s.split(" "); System.out.println( removeDupes( wDupes, "time")); System.out.println( removeDupes( wDupes)); } private static String removeDupes(String[] s, String search) { String result = ""; for (int i = 0; i < s.length; i++) { if (!search.equalsIgnoreCase(s[i]))

{ result += s[i] + " "; } } return result.substring(0, result.lastIndexOf(" ")); } // this method would leave only 1 copy of a word... private static String removeDupes(String[] s) { String noDupes = ""; for (int i = 0; i < s.length; i++) { if (noDupes.indexOf(s[i]) < 0) { // -1 otherwise, if not in noDupes noDupes += s[i] + " "; } } return noDupes; } } //letter avg import java.util.Scanner; import java.util.*; public class LetterCount2 { public static void main (String[] args) { Scanner scan = new Scanner (System.in); String upper =""; String lower =""; String other =""; int n; float avg=0; System.out.print("Enter a String: "); String inputstring = scan.nextLine(); StringTokenizer st=new StringTokenizer(inputstring); n=st.countTokens(); for (int i = 0; i < inputstring.length(); i++) { char thisChar = inputstring.charAt(i); if (thisChar >= 65 && thisChar <= 90) { upper += thisChar; } else if (thisChar >= 97 && thisChar <= 122) {

lower += thisChar; } int a=upper.length(); int b =lower.length(); avg = (a+b)/n; } System.out.println("NO. OF UPPER CASE LETTER = " + upper.length() + " ---" + upper); System.out.println("NO. OF LOWER CASE LETTER= " + lower.length() + " --- " + lower); System.out.println("NO. OF WORDS = "+n); System.out.println("AVERAGE OF LOWER AND UPPER LETTER PER WORD =" + avg); } }

// tranpose matrix columns and rows import java.text.*; // fancy formatting public class transpose { static boolean trans(int m[][]) { boolean flag = true; int temp; int rSize=m.length; int cSize=m[0].length; // get dimensions if (rSize!=cSize){return false;} // not a square matrix for (int r = 0; r < rSize; r++) { for (int c = r; c < cSize; c++) // to prevent double swap { temp=m[r][c];m[r][c]=m[c][r];m[c][r]=temp; } } return flag; } static void show(int a[][]) { int rSize=a.length; int cSize=a[0].length; // get dimensions DecimalFormat df = new DecimalFormat("000"); for (int r=0; r < rSize; r++) {for (int c=0; c < cSize; c++) {System.out.print(" "+df.format(a[r][c]));} System.out.println();}

System.out.println(); // just for visibility } public static void main(String args[]) { int M[][] = new int[5][5]; // create symetric array for (int row=0; row < 5; row++) // fill with data {for (int col=1; col < 5; col++) {M[row][col] = row+col;}} M[2][1] = 9; // make sure it isnt symetric System.out.println(); show(M); // display original // display transposition results if (trans(M) == true) {show(M);} else {System.out.println("Object is NOT square/n");} } } //longest word import java.util.*; import java.io.*; class LongestWord2 { public static void main(String args[])throws IOException { InputStreamReader ir=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(ir); String word, longestWord = ""; System.out.print("Enter sentence: "); Scanner console = new Scanner(System.in); String sentense = console.nextLine(); Scanner console2 = new Scanner(sentense); while ( console2.hasNext() ) { word=console2.next(); if (word.length() > longestWord.length()) longestWord = word; } System.out.print("The longest word was: " + longestWord); }} //sales report import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.text.DecimalFormat;

import java.text.NumberFormat; public class SalesReport { public static void main(String[] args) { Float[][] sales = new Float[5][4]; BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); String userInput; for (int sp = 0; sp < 4; sp++) { for (int prod = 0; prod < 5; prod++) { while (sales[prod][sp] == null) { try { System.out.println("Enter the Amount of sales for sales person: " + String.valueOf(sp + 1) + " and product: " + String.valueOf(prod + 1)); userInput = stdin.readLine(); sales[prod][sp] = Float.valueOf(userInput); } catch (IOException e) { e.printStackTrace(); } catch (NumberFormatException e1) { System.out.println("Invalid amount"); } } } } System.out.print("Salesman:"); for (int sp = 0; sp < 4; sp++) { if (sp > 0) { System.out.print('\t'); } System.out.print('\t'); System.out.print(String.valueOf(sp + 1)); } System.out.print("\t\tProduct Total\n"); // Number format will be used for printing values in a user readable format NumberFormat format = new DecimalFormat("RS0,000.00"); // Initialize variables for totals float grandtotal = 0f; float producttotal; float[] salespersontotal = new float[4]; // Print rows and calculate sales person and grand totals for (int prod = 0; prod < 5; prod++) {

producttotal = 0f; System.out.print("Product "); System.out.print(String.valueOf(prod + 1)); System.out.print(": "); for (int sp = 0; sp < 4; sp++) { // Calculate grand, product, and sales person totals grandtotal += sales[prod][sp]; producttotal += sales[prod][sp]; salespersontotal[sp] += sales[prod][sp]; // Output amount sold of product by sales person System.out.print('\t'); System.out.print(format.format(sales[prod][sp])); } // Output product totals System.out.print('\t'); System.out.print(format.format(producttotal)); System.out.print('\n'); } // Output sales person totals System.out.print("Totals:\t"); for (int sp = 0; sp < 4; sp++) { System.out.print('\t'); System.out.print(format.format(salespersontotal[sp])); } // Output grand total System.out.print('\t'); System.out.print(format.format(grandtotal)); System.out.print('\n'); } }

Anda mungkin juga menyukai