Created
March 22, 2015 20:26
-
-
Save ad-m/9fa09aebc5bd0788b7f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Zadanie 5 | |
| Program generuje liczby całkowite do macierzy o | |
| wymiarze 10x10, a następnie oblicza i wyświetla | |
| sumę liczb na przekątnych macierzy. */ | |
| public class FiveArray extends Matrix{ | |
| //int[][] data; | |
| //int w; | |
| //int h; | |
| public FiveArray(int h) { | |
| super(h, h); | |
| } | |
| public static void main(String[] args) { | |
| Matrix data = new FiveArray(10); | |
| data.print(); | |
| System.out.print("Diagonal is: " + data.diagonal()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Random; | |
| /* Zadanie 4 | |
| Dla tablicy o rozmiarze 5 x 5 opracuj metody: | |
| A. inicjującą tablicę losowymi liczbami całkowitymi | |
| należącymi do przedziału [-100, 100); | |
| B. wyprowadzającą zawartośd tablicy wierszami na ekran; | |
| C. obliczającą sumę elementów należących do wiersza o | |
| numerze i, gdzie 1 <= i <= 5; | |
| D. obliczającą sumę elementów należących do kolumny o | |
| numerze j, gdzie 1 <= j <= 5; | |
| E. sortujący rosnąco każdy z wierszy tablicy; | |
| F. sortujący malejąco każdą z kolumn tablicy; | |
| G. znajdującą w tablicy element minimalny i maksymalny. */ | |
| public class FourArray extends Matrix { | |
| private static final Random rand = new Random(); | |
| public FourArray(int h, int w) { | |
| super(h, w); | |
| for(int i=0; i<w; i++){ | |
| for(int j=0; j<h; j++){ | |
| data[i][j] = rand.nextInt()%200-100; | |
| } | |
| } | |
| } | |
| public static void main(String[] args) { | |
| FourArray instance = new FourArray(5, 5); | |
| instance.print(); | |
| System.out.println("instance.SumRow(2): " + instance.sumRow(2)); | |
| System.out.println("instance.SumColumn(2):" + instance.sumColumn(2)); | |
| System.out.println("instance.sortRow(2): "); | |
| instance.sortRow(2); | |
| instance.print(); | |
| System.out.println("instance.sortColumn(2): "); | |
| instance.sortColumn(2); | |
| instance.print(); | |
| System.out.println("instance.Max(): " + instance.max()); | |
| System.out.println("instance.Min(): " + instance.min()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Matrix { | |
| public int data[][]; | |
| public int w; | |
| public int h; | |
| public Matrix(int w, int h) { | |
| this.w = w; | |
| this.h = h; | |
| this.data = new int[w][h]; | |
| } | |
| public void print() { | |
| for(int i=0; i<w; i++){ | |
| for(int j=0; j<h; j++){ | |
| System.out.print(data[i][j] + "\t"); | |
| } | |
| System.out.print("\n"); | |
| } | |
| } | |
| public int sumRow(int i) { | |
| int sum = 0; | |
| for(int j=0; j<h; j++){ | |
| sum+=data[i][j]; | |
| } | |
| return sum; | |
| } | |
| public int sumColumn(int j) { | |
| int sum = 0; | |
| for(int i=0; i<w; i++){ | |
| sum+=data[i][j]; | |
| } | |
| return sum; | |
| } | |
| public void sortRow(int i) { | |
| int tmp; | |
| for(int j=0; j<h-1; j++){ | |
| if(data[i][j]>data[i+1][j]){ | |
| tmp = data[i+1][j]; | |
| data[i+1][j] = data[i][j]; | |
| data[i][j] = tmp; | |
| } | |
| } | |
| } | |
| public void sortColumn(int j) { | |
| int tmp; | |
| for(int i=0; i<w-1; i++){ | |
| if(data[i][j]>data[i+1][j]){ | |
| tmp = data[i+1][j]; | |
| data[i+1][j] = data[i][j]; | |
| data[i][j] = tmp; | |
| } | |
| } | |
| } | |
| private int find(boolean max) { | |
| int r=data[0][0]; | |
| for(int i=0 ; i<w; i++){ | |
| for(int j=0; j<h; j++){ | |
| if((max && r<data[i][j]) || | |
| (!max && r>data[i][j])){ | |
| r = data[i][j]; | |
| } | |
| } | |
| } | |
| return r; | |
| } | |
| public int max() { | |
| return this.find(true); | |
| } | |
| public int min() { | |
| return this.find(false); | |
| } | |
| public int diagonal() { | |
| int sum=0; | |
| for(int i=0; i<w; i++){ | |
| sum+=data[i][i]; | |
| } | |
| return sum; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.MissingResourceException; | |
| import java.util.ResourceBundle; | |
| public class Messages { | |
| private static final String BUNDLE_NAME = "messages"; //$NON-NLS-1$ | |
| private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle | |
| .getBundle(BUNDLE_NAME); | |
| private Messages() { | |
| } | |
| public static String getString(String key) { | |
| try { | |
| return RESOURCE_BUNDLE.getString(key); | |
| } catch (MissingResourceException e) { | |
| return '!' + key + '!'; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| /* Zadanie 2 | |
| Program wczytuje do tablicy 5 nazwisk i wypisuje te, | |
| które nie zaczynają się od liter od A do K, a | |
| następnie wszystkie dłuższe, niż 5-cio znakowe. */ | |
| public class Name { | |
| String text; | |
| public Name(String text) { | |
| super(); | |
| this.text = text; | |
| } | |
| public boolean checkStartAndEnd(char start, char end){ | |
| return (this.text.charAt(0) == start && | |
| this.text.charAt(this.text.length()) == end); | |
| } | |
| public int length(){ | |
| return this.text.length(); | |
| } | |
| public void print(){ | |
| System.out.println(this); | |
| } | |
| @Override | |
| public String toString() { | |
| return "Name [text=" + text + "]"; | |
| } | |
| public static void main(String[] args) { | |
| Scanner sc = new Scanner(System.in); | |
| int count= 5; | |
| Name[] rows = new Name[count]; | |
| for(int i=0; i<count; i++){ | |
| rows[i] = new Name(sc.nextLine()); | |
| } | |
| System.out.print("Start A and end K has been in words:"); | |
| for(Name el: rows){ | |
| if(!el.checkStartAndEnd('A', 'K')){ | |
| el.print(); | |
| } | |
| } | |
| System.out.println("All longer than 5 chars:"); | |
| for(Name el: rows){ | |
| if(el.length()>5){ | |
| el.print(); | |
| } | |
| } | |
| sc.close(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Zadanie 3 | |
| Napisz program wypełniający tablicę 10x10 wartościami 0 lub 1, który | |
| będzie nadawał elementowi [i][j] wartośd 1 jeżeli największy | |
| wspólny dzielnik i oraz j wynosi 1, a 0 w przeciwnym wypadku. | |
| Program powinien wypisad wartości tablicy w kształcie 10x10. */ | |
| public class TenArray{ | |
| boolean[][] array; | |
| int w; | |
| int h; | |
| public TenArray(int w, int h) { | |
| this.w = w; | |
| this.h = h; | |
| this.array = new boolean[w][h]; | |
| for (int i = 0; i < w; i++) { | |
| for (int j = 0; j < h; j++) { | |
| this.array[i][j] = this.check(i, j); | |
| } | |
| } | |
| } | |
| public boolean check(int i, int j) { | |
| if(i == 0){ return (j == 1 ? true : false); }; | |
| if(j == 0){ return (i == 1 ? true : false); }; | |
| while (i != j) { | |
| if (i > j) { | |
| i -= j; | |
| } else { | |
| j -= i; | |
| } | |
| } | |
| return (i == 1 ? true : false); | |
| } | |
| public void print() { | |
| for (int i = 0; i < w; i++) { | |
| for (int j = 0; j < h; j++) { | |
| System.out.print(this.array[i][j] ? "0 " : "1 "); | |
| } | |
| System.out.print("\n"); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| TenArray data = new TenArray(10, 10); | |
| data.print(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Zadanie 1 | |
| Program wczytuje słowa do tablicy i wyświetla wszystkie | |
| występujące w nich palindromy. Liczba wczytywanych słów | |
| zależy od wartości parametru wprowadzonego z klawiatury | |
| przez użytkownika. */ | |
| import java.util.Scanner; | |
| public class Word { | |
| String text; | |
| public Word(String text) { | |
| this.text = text; | |
| } | |
| public boolean checkPalindrome(){ | |
| for(int i=0; i<(text.length()/2); i++){ | |
| if(text.charAt(i) != text.charAt(text.length()-i-1)){ | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| public void print(){ | |
| System.out.println(this); | |
| } | |
| @Override | |
| public String toString() { | |
| return "Word [text=" + text + "]"; | |
| } | |
| public static void main(String[] args) { | |
| Scanner sc = new Scanner(System.in); | |
| System.out.println("Enter wordds number:"); | |
| int count= sc.nextInt(); | |
| Word[] words = new Word[count]; | |
| for(int i=0; i<count; i++){ | |
| System.out.println("Enter word no." + i + ":"); | |
| words[i] = new Word(sc.nextLine()); | |
| } | |
| for(Word el: words){ | |
| if(el.checkPalindrome()){ | |
| el.print(); | |
| } | |
| } | |
| sc.close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment