0% found this document useful (0 votes)
2 views7 pages

Java Test Helper

The document provides a collection of useful Java functions for manipulating strings and arrays, including conversions, character counting, and checking for subsequences. It also includes examples of basic class structures, such as a Person class and a Pair class, along with methods for handling dates and entries in a MultiTicket class. Additionally, it contains reminders for common string and array operations in Java.

Uploaded by

talginzburg9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Java Test Helper

The document provides a collection of useful Java functions for manipulating strings and arrays, including conversions, character counting, and checking for subsequences. It also includes examples of basic class structures, such as a Person class and a Pair class, along with methods for handling dates and entries in a MultiTicket class. Additionally, it contains reminders for common string and array operations in Java.

Uploaded by

talginzburg9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Test Helper – Strings, Arrays &

Classes
1. Useful String Functions

String ↔ Char Array


char[] arr = [Link](); // String to char[]
String str = new String(arr); // char[] to String

String → Array of Words


String[] words = [Link](" ");

Count specific character


public static int countChar(String str, char ch) {
int count = 0;
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == ch) count++;
}
return count;
}

Check if string has only digits


public static boolean onlyDigits(String str) {
for (int i = 0; i < [Link](); i++) {
if (![Link]([Link](i))) return false;
}
return true;
}

2. Useful Array Functions

Check if array is subsequence (‫ברצף‬-‫שלא‬-‫)מוכל‬


public static boolean isSubsequence(char[] small, char[] big) {
int j = 0;
for (int i = 0; i < [Link] && j < [Link]; i++) {
if (big[i] == small[j]) j++;
}
return j == [Link];
}

Reverse an array
public static void reverse(int[] arr) {
for (int i = 0; i < [Link] / 2; i++) {
int temp = arr[i];
arr[i] = arr[[Link] - 1 - i];
arr[[Link] - 1 - i] = temp;
}
}

Max value index


public static int maxIndex(int[] arr) {
int max = arr[0], index = 0;
for (int i = 1; i < [Link]; i++) {
if (arr[i] > max) {
max = arr[i];
index = i;
}
}
return index;
}

3. Classes – Examples & Functions

A Basic Class: Person

public class Person {


private String name;
private int age;

// Constructor
public Person(String name, int age) {
[Link] = name;
[Link] = age;
}

// Getters and Setters


public String getName() { return name; }
public void setName(String name) { [Link] = name; }
public int getAge() { return age; }
public void setAge(int age) { [Link] = age; }
}

4. Final Reminders & Snippets

- [Link](i) – get character at index.


- [Link]() – string length.
- [Link] – array length.
- [Link](other) – string comparison.
- [Link](ch) – is it a digit?
- [Link](ch) – is it a letter?
- new String(charArray) – convert array to string.
- [Link]() – convert string to char array.

Example Matkonet 2023:

1. ‫מוכל לא ברצף‬

public static boolean isOrderlyContained(char[] a, char[] b) {

int i = 0, j = 0;

while (j < [Link] && i < [Link]) {

if (a[i] == b[j])

i++;

j++;

return (i == [Link]);

{
2. Pair:

Pair:

private int num;

private int digits;

public int getNum() {

return num;

public int getDigits() {

return digits;

public Pair(int digits) {

[Link] = digits;

num = 0;

int d = (int) ([Link]() * 9) + 1;

int m = 1;

for (int i = 0; i < digits; i++) {

num = num + m * d;

m = m * 10;

}
Main:

static Scanner input = new Scanner([Link]);

public static Pair[] getPairs() {

Pair[] a = new Pair[20];

[Link]("Please enter 20 numbers, each in range 1-7:");

for (int i = 0; i < [Link]; i++) {

a[i] = new Pair([Link]());

return a;

/*

* Using ">=" to return the last maximum we find

*/

public static int maxDigits(Pair[] a) {

int imax = 0;

for (int i = 1; i < [Link]; i++)

if (a[i].getDigits() >= a[imax].getDigits())

imax = i;

return a[imax].getNum();

3. MultiTicket, MyDate

MyDate:
public class MyDate {

private int day;

private int month;

private int year;

public MyDate(int day, int month, int year) {

[Link] = day;

[Link] = month;

[Link] = year;

public int diffDays (MyDate date) { return -1; }

MultiTicket:

private String owner;

private MyDate start;

private MyDate [] entries;

private int current;

public int EntriesInMonth (int month);

public boolean AddEntry(MyDate entry) {

if (current < 30 && [Link](start) <= 365) {

entries[current] = entry;

current++;
return true;

return false;

Main:

public static int total(MultiTicket[] a, int month) {

int sum = 0;

for (int i = 0; i < [Link]; i++) {

sum += a[i].EntriesInMonth(month);

return sum;

You might also like