//Import class import java.time.Year; import java.util.Scanner; public class konsonanvokal{ public static void main(String []args){ //Paparan output-maklumanawal aturcara System.out.println("ATURCARA BILANG HURUF KONSONAN & VOKAL"); //Pengisytiharan pembolehubah Scanner Scanner taip =new Scanner(System.in); //Papar teks pertanyaaan dan istihar pembolehubah System.out.println("Taipkan satu perkataandan tekan ENTER"); String perkataan = taip.next(); char[] hrurufv = perkataan.toCharArray(); int vokal = 0; //Kawalan ulangan for for (char h: hurufv) { if(h == 'a' || h == 'A' || h == 'e' || h == 'E'|| h == 'i' || h== 'I' || h == 'o'|| h == 'O' || h == 'u' || h == 'U') { //Penambah vokal++; } } //Paparan output System.out.println("Bilangan huruf vokal dalam " + perkataan + " adalah: " + vokal); System.out.println("Bilangan huruf konsonan dalam " + perkataan + " adalah: " + ( hurufv.length - vokal)); } }
public class GroceryCosts{ public static void main(String []args){ int[][] prices = { {3, 5, 3}, {8, 9, 7}, {1, 8, 8}, }; int total = 0; for (int i=0;i<=2;i++) { int totalFoodLion = 0; int totalHarrisTeeter = 0; int totalWalMart = 0; for(int j=0;j<=2;j++) { System.out.print("$" + prices[i][j] + " "); totalFoodLion = totalFoodLion + prices[i][j]; totalHarrisTeeter = totalHarrisTeeter + prices[i][j]; totalWalMart = totalWalMart + prices[i][j]; total = total + prices[i][j]; } if(i==0) System.out.println("Total cost for Food Lion is $" + totalFoodLion); if(i==1) System.out.println("Total cost for Harris Teeter is $" + totalHarrisTeeter); if(i==2) System.out.println("Total cost for WalMart is $" + totalWalMart); } System.out.println("Total cost for all stores is $" + total); } }
import java.util.ArrayList; //CREATED BY FILIP SAULEAN FOR CS125 //4/10/2019 class sigmaStar { int length = 0; ArrayList<ArrayList<Integer>> setForLength; int iteration = 0; void powerSet() { setForLength = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> originalArray = new ArrayList<>(); setForLength.add(new ArrayList<Integer>()); for(int i = 0; i < length; i++) { originalArray.add(i); } powerSetHelper(new ArrayList<Integer>(), originalArray, 0); } void powerSetHelper(ArrayList<Integer> array, ArrayList<Integer> originalArray, int index) { int i = index; while(i < originalArray.size()) { ArrayList<Integer> temp = copy(array); temp.add(originalArray.get(i)); setForLength.add(temp); i++; powerSetHelper(temp, originalArray, i); } } //to make a deep copy ArrayList<Integer> copy(ArrayList<Integer> arrayToBeCopied) { ArrayList<Integer> copiedArray = new ArrayList<>(); for(int value : arrayToBeCopied) { copiedArray.add(value); } return copiedArray; } String next () { String x1 = ""; //when we move onto the strings of a length one greater than the previous string if (iteration == 0) { powerSet(); } for(int i = 0; i < length; i++) { if(setForLength.get(iteration).contains(i)) { x1 += "b"; } else { x1 += "a"; } } iteration++; if (iteration == Math.pow(2, length) || length == 0) { length++; iteration = 0; } return x1; } } public class Main { public static void main(String[] args) { sigmaStar s = new sigmaStar(); for(int i = 0; i < 220; i++) { System.out.println("STRING " + (i + 1) + " : " + s.next()); } } }
public class Substring { public static void maxBitonicSubstring(int[] arr) { //longest increasing subarray int[] increase = new int[arr.length]; increase[0] = 1; for (int i = 1; i < arr.length; i++) { increase[i] = 1; if (arr[i - 1] < arr[i]) { increase[i] = increase[i - 1] + 1; } } //longest decreasing subarray int[] decrease = new int[arr.length]; decrease[arr.length - 1] = 1; for (int i = arr.length - 2; i >= 0; i--) { decrease[i] = 1; if (arr[i] > arr[i + 1]) { decrease[i] = decrease[i + 1] + 1; } } //find max Bitonic subarray int maxLen = 1; int start = 0, end = 0; for (int i = 0; i < arr.length; i++) { if (maxLen < increase[i] + decrease[i] - 1) { maxLen = increase[i] + decrease[i] - 1; start = i - increase[i] + 1; end = i + decrease[i] - 1; } } //return array int[] bitonic = new int[end - start + 1]; for (int i = start; i <= end; i++) { System.out.print(arr[i] + " "); } } public static void main(String[] args) { int[] A = { 3, 5, 8, 4, 5, 9, 10, 8, 5, 3, 4 }; maxBitonicSubstring(A); } }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Labib */ import java.util.Scanner; public class HW8_PlaceQueens { public static void main(String[] args) { Scanner kb = new Scanner(System.in); String option = "start"; while (!option.equals("q")) { System.out.println("q - quit"); System.out.println("r - reset"); System.out.println("m - make"); System.out.println("a - add queen"); System.out.println("d - delete queen"); System.out.println("c - count queen"); System.out.println("p - print board\n"); System.out.print("Enter option: "); option = kb.next().toLowerCase(); int[][] userboard = null; if (option.equals("r")) { System.out.print("Enter board size "); int boardSize = kb.nextInt(); PrintBoard(MakeBoard(boardSize)); } if (option.equals("m")) { System.out.print("Enter board size "); int boardSize = kb.nextInt(); userboard = MakeBoard(boardSize); PrintBoard(userboard); } else if (option.equals("a")) { System.out.print("Enter row and column (1-N): "); int userRow = kb.nextInt(); int userCol = kb.nextInt(); userboard = AddQueen(userRow, userCol, userboard); PrintBoard(userboard); } } } public static int[][] MakeBoard(int size) { int[][] res = new int[size][size]; return res; } public static int[][] AddQueen(int x, int y, int[][] board) { int xPos = x - 1; int yPos = y - 1; board[xPos][yPos] = 1; int row, col; for (row = 0; row < board.length; row++) { for (col = 0; col < board[row].length; col++) { if (row == xPos) { board[row][col] = 2; } if (col == yPos) { board[row][col] = 2; } } } board[xPos][yPos] = 1; return board; } public static void PrintBoard(int[][] table) { int row, col; for ( int i = 0; i <= table.length; i ++) { System.out.printf("%3d|", i); } System.out.println(); System.out.println(DottedLine(table.length)); for (row = 0; row < table.length; row++) { System.out.printf("%3d|", row + 1); for (col = 0; col < table[row].length; col++) { if (table[row][col] == 0) { System.out.print(" |"); } else if (table[row][col] == 1) { System.out.print(" Q |"); } else { System.out.print(" * |"); } } System.out.println(); System.out.println(DottedLine(table[row].length)); } } public static String DottedLine(int n) { String res = ""; for (int i = 0; i <= n; i++ ) { res += "----"; } return res; } }
public class Example{ public static void main(String args[]) { String Flower ="Tulip "; char add = 'i'; String Floweradd = Flower + add; System.out.println("the length is: "+ Flower.length() + " The char in the middle is " + Flower.charAt(2)); } }
import java.io.PrintStream; import java.util.Scanner; public class Torrey_Izabella_Phonebook { public static void main(String[] args) { Scanner reader = new Scanner (System.in); String userinput = ""; char usercommand = ' '; while (usercommand != 'q' && usercommand != 'Q') { System.out.println("Command: "); userinput = reader.nextLine(); usercommand = userinput.trim().charAt(0); int num_entries = 0; switch (usercommand) { case 'l': System.out.println(); case 'f': case 'e': num_entries++; } userinput.substring(1, userinput.length() - 1); } } public static void readPhonebook(String filename) throws Exception{ } public static void storePhonebook(String filename, int num_entries) throws Exception{ PrintStream P = new PrintStream(filename); for (int i = 0; i < num_entries; i++) { P.println(EntryList[i].name + "\t" + EntryList[i].number + "\t" + EntryList[i].notes + "\t"); } P.close(); System.out.println("Phonebook stored."); } public static void ListAllEntries() { System.out.println(); } } class Entry { String name; String number; String notes; } class EntryList { public Entry[] entries; public String filename; public EntryList{ this.entries = new Entry[200]; this.filename = filename; Torrey_Izabella_Phonebook.readPhonebook(this.filename); } }
import java.util.Arrays; public class moddedSort { public static void selectionSort(String[] array) { System.out.println("Unsorted selectionSort: " + Arrays.toString(array)); for(int i = 0; i <= array.length - 1; i++) { int max = i; for(int j = i + 1; j < array.length; j++) { if(array[j].compareTo(array[max]) > 0) { max = j; } } String temp = array[i]; array[i] = array[max]; array[max] = temp; } System.out.println("Sorted selectionSort: " + Arrays.toString(array)); } public static void insertionSort(String [] array) { System.out.println("Unsorted insertionSort: " + Arrays.toString(array)); String k; int i = 0; for(int j = 1; j <array.length; j++) { k = array[j]; i = j - 1; while(i >= 0) { if(k.compareTo(array[i]) > 0) { break; } array[i + 1] = array[i]; i--; } array[i + 1] = k; } System.out.println("Sorted insertionSort: " + Arrays.toString(array)); } public static void dualSelectSort(int [] array) { System.out.println("Unsorted dualSelectSort: " + Arrays.toString(array)); for (int i = 0; i < array.length / 2; i++) { int min = i; int max = array.length - 1 - i; for (int j = i; j < array.length - i; j++) { if (array[j] < array[min]) { min = j; } if (array[j] > array[max]) { max = j; } } if (max == i) { max = min; } } } public static void main(String [] args) { String [] a = {"a","q","b","z","f"}; String [] b = {"r","w","i","x","j","e"}; int [] c = {25, 3, 76, 7, 43, 82, 56, 97}; selectionSort(a); System.out.println(); insertionSort(b); System.out.println(); dualSelectSort(c); } }
public class Examen{ public static void main(String []args){ int array_1[][]=new int[3][3]; int array_2[][]=new int[3][3]; int array_3[][]=new int[3][3]; int array_4[][]=new int[3][3]; int Suma=0; for(int a=0; a<=3; a++) { for(int b=0; b<=3; b++) { array_1[a][b]=Suma++; for(int G=0; G<=5; G++) { System.out.print(array_1[a][b]=Suma++); } } } for(int c=0; c<=3; c++) { for(int d=0; d<=3; d++) { array_2[c][d]=array_1[c][d]*5; for(int Igual=0; Igual<=5; Igual++) { System.out.print(array_2[c][d]=array_1[c][d]*5); } } } for(int e=0; e<=3; e++) { for(int f=0; f<=3; f++) { array_3[e][f]=array_2[e][f]-3; for(int Min=0; Min<=5; Min++) { System.out.print(array_3[e][f]=array_2[e][f]-3); } } } int array1[][]=new int [3][3]; int array2[][]=new int [3][3]; int array3[][]=new int [3][3]; int array4[][]=new int [3][3]; int array5[][]=new int [3][3]; int array6[][]=new int [3][3]; for(int g=0; g<=5; g++) { for(int h=0; h<=5; h++) { array1[g][h]=14; for(int A=0; A<=5; A++) { System.out.print(array1[g][h]=14); if(array1[g][h]==14) { System.out.print("Numero localizado"); } else { System.out.print("Numero no registrado"); } } } } for(int i=0; i<=5; i++) { for(int j=0; j<=5; j++) { array1[i][j]=33; for(int B=0; B<=5; B++) { System.out.print(array2[i][j]=33); if(array2[i][j]==33) { System.out.print("Numero localizado"); } else { System.out.print("Numero no registrado"); } } } } for(int k=0; k<=5; k++) { for(int l=0; l<=5; l++) { array1[k][l]=11; for(int C=0; C<=5; C++) { System.out.print(array3[k][l]=11); if(array1[k][l]==11) { System.out.print("Numero localizado"); } else { System.out.print("Numero no registrado"); } } } } for(int m=0; m<=5; m++) { for(int n=0; n<=5; n++) { array1[m][n]=8; for(int M=0; M<=5; M++) { System.out.print(array4[m][n]=8); if(array1[m][n]==8) { System.out.print("Numero localizado"); } else { System.out.print("Numero no registrado"); } } } } for(int o=0; o<=5; o++) { for(int p=0; p<=5; p++) { array1[o][p]=4; for(int M=0; M<=5; M++) { System.out.print(array5[o][p]=4); if(array1[o][p]==4) { System.out.print("Numero localizado"); } else { System.out.print("Numero no registrado"); } } } } } }
public class HelloWorld{ public static void main(String []args){ int[] input = new int[] {3,4,31,0,-4}; input = sortare(input); for (int i = 0; i < input.length; i++) { System.out.println("" + input[i]); } } static int[] sortare(int []a){ for (int i = 0; i < a.length-1; i++) { int min = a[i]; int poz = i; for (int k = i+1; k < a.length; k++) { if (a[k] < min) { min=a[k]; poz=k; } } int aux = 0; aux = a[i]; a[i]=a[poz]; a[poz]=aux; } return a; } }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more