0% found this document useful (0 votes)
6 views

Homework Methods

The document contains code for several methods that perform various tasks related to strings, numbers, and date/time in Java. Specifically, it includes methods to: 1) Return the name of a month given its number between 1-12 or return an error if invalid. 2) Count the vowels and consonants in a string by passing the string to methods. 3) Generate a random integer within a given range. 4) Check if a character is a vowel. 5) Find the maximum of 3 numbers.

Uploaded by

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

Homework Methods

The document contains code for several methods that perform various tasks related to strings, numbers, and date/time in Java. Specifically, it includes methods to: 1) Return the name of a month given its number between 1-12 or return an error if invalid. 2) Count the vowels and consonants in a string by passing the string to methods. 3) Generate a random integer within a given range. 4) Check if a character is a vowel. 5) Find the maximum of 3 numbers.

Uploaded by

Adriana Mocanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

package intro;

import java.time.Month;
import java.util.Random;

public class HomeworkMethods {


public static void main(String[] args) {

//afiseaza doar lunile de la 1 la 12 (ce e in afara da


"DateTimeException")
// Month month = Month.of(20);
// System.out.println(month);

// System.out.println(month(s));
// System.out.println(luna(20));

// System.out.println("Number of vowels: " + vowelsCount("Test


automation is wonderful"));
// System.out.println("Number of consonant: " + consonantCount("Test
automation is wonderful"));

computeVowelsAndConsonates("ab !+");

//System.out.println(isEven(2));

// int sum = oddSum(10);


// int prod = evenProduct(10);
// System.out.println("Suma primelor 10 de numere impare:" + sum +"
Produsul primelor 10 de numere pare:" + prod);

System.out.println(max (5,8,3));

System.out.println(isVowel ('I'));

// System.out.println(getRandom (100));
//
// System.out.println(getRandom2 (4,10));

String string = "test automation is wonderful";


System.out.println("The string has " + words(string) + " words.");

//8. Scrie o metoda java care calculeaza cate cuvinte sunt intr-un string.
public static int words(String string) {
int count = 0;

for (int i = 0; i < string.length(); i++) {

if (string.charAt(i) != ' ') {


count++;
// aici verificam daca ce scriem noi este diferit de
spatiu
}
while (string.charAt(i) != ' ' && i < string.length() - 1)
i++;
// aici continuam sa navigam prin string pana dam de un
spatiu; ce e dupa un spatiu reprezinta un cuvant
// "string.length() - 1" este ultimul caracter dintr-un
string
}

return count;
}

//7. Genereaza random un nr intreg intr-un anumit range. e.g. intre 4 si


10 , valori variabile.

public static int getRandom2 (int min, int max) {

Random random = new Random();


return random.nextInt((max - min) + 1) + min;

//explicatia lui "((max - min) + 1) + min" :


// ((10 - 4) + 1) + 4
// (6 + 1) + 4 = (7) + 4
// [0...6] + 4
// 0 + 4 = 4
// 1 + 4 = 5
// ......
// 6 + 4 = 10

// Ex 2: ((16-15) + 1) + 15
// (1 + 1 ) + 15 = (2) + 15
// [0....1] + 15
// 0 + 15 = 15
// 1 + 15 = 16

// Ex 3: ((20-10) + 1) + 10
// (10 + 1) + 10 = (11) + 10
// [0....10] + 10
// 0 + 10 = 10
// 1 + 10 = 11
// 2 + 10 = 12
// ......
// 10 + 10 = 20

// Ex 4: ((100 - 0) + 1) + 0
// (100 + 1) + 0 = (101) + 0
// [0...100] + 0
// 0 + 0 = 0
// 1 + 0 = 1
// ......
// 100 + 0 = 100

// 6. Genereaza random un nr intreg, maxim pana la 100

public static int getRandom(int max) {

Random random = new Random();


return random.nextInt(max);

//5.Creaza o metoda care returneaza true daca un anumit caracter e vocala


si false daca nu e vocala.

// public static boolean isVowel(char caracter) {


//
// caracter = Character.toLowerCase(caracter);
//
// if ((caracter == 'a' || caracter == 'e' || caracter == 'i'
|| caracter == 'o' || caracter == 'u')) {
// return true;
//
// }
// return false;
// }

//4. Defineste o metoda care calculeaza maximul dintre 3 numere date.

// public static int max(int a, int b, int c) {


//
// if (a > b && a > c) {
// return a;
// } else if (b > c) {
// return b;
// }
// return c;
// }

//metoda de la Codruta
public static String max(int a, int b, int c) {

if (a >= b && a >= c) {


return "max este a, cu valoarea: " + a;
}
if (b >= a && b>= c) {
return "max este b, cu valoarea: " + b;
}
return "max este c, cu valoarea: " + c;
}

// if (a > b && a > c)


// return a;
// if (b > a && b > c)
// return b;
// return c;
// }

//3.Rezolva problema de data trecuta folosind metode (cel putin 3) (I take


into consideration the numbers from 0 to 100. For odd numbers, compute their sum,
for even numbers,
// compute their product.)

//metoda care calculeaza suma primelor n numere impare


//am folosit in loc de (nr%2 != 0) -> !isEven(i)
public static int oddSum(int nr) {

int sum = 0;

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

if (!isEven(i)) {
sum = sum + i;
}
}
return sum;
}

//metoda care calculeaza produsul primelor n numere pare


//am folosit in loc de (nr%2 == 0) -> isEven(i)
public static int evenProduct(int nr) {

int prod = 0;

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


if (isEven(i)) {
prod = prod * i;
}
}
return prod;
}
//metoda care verifica daca un numar este par sau impar

public static boolean isEven(int nr) {


if (nr % 2 == 0) {
return true;
} else
return false;
}

//metoda de la Codruta

// public static void calc () {


//
// int s = 0;
// int p = 1;
//
// for (int i=1;i<=10;i++) {
// if (isEven(i)) {
// p = produs(p,i);}
// else {
// s = suma(s,i);
// }
// }
// }

// public static int suma (int a, int b) {


// return a +b;
// }
// public static int produs (int a, int b) {
// return a*b;
// }
// public static boolean isEven (int n) {
// if (n%2==0) return true;
// else return false;
// }

//2. Rezolva problema de data trecuta folosind metode (cel putin 2)


(Having a quote as string: "Test automation is wonderful". Compute how many vowels
and consonant exists.)

// metoda pentru a calcula toate consoanele dintr-un string format numai


din litere si spatii
// public static int consonantCount(String quote) {
//
// int consonantCount = 0;
// quote = quote.toLowerCase();
//
// for (int i = 0; i < quote.length(); i++) {
// char ch = quote.charAt(i);
//
// if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' &&
ch != 'u' && ch !=' ') { //am scos si spatiile
// consonantCount++;
// }
// }
// return consonantCount;
// }

// metoda pentru a calcula toate vocalele dintr-un string


// public static int vowelsCount(String quote) {
//
// int vowelsCount = 0;
// quote = quote.toLowerCase();
//
// for (int i = 0; i < quote.length(); i++) {
// char ch = quote.charAt(i);
//
// if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch
== 'u') {
// vowelsCount++;
// }
// }
// return vowelsCount;
// }

//metoda de la Codruta

public static void computeVowelsAndConsonates(String text) {


int c = 0;
int v = 0;
for (int i = 0; i < text.length(); i++) {
if (isLetter(text.charAt(i))) {
if (isVowel(text.charAt(i))) {
v++;
} else {
c++;
}
}
}
System.out.println("vocale: " + v+ " consoane: " +c);
}

public static boolean isLetter (char c) {


if (c >= 'a' && c <= 'z') return true; //vedem daca e caracter sau nu
else return false;
}

public static boolean isVowel(char c) {


boolean rez = false;

c = Character.toLowerCase(c);

if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {


rez = true;
}

return rez;
}

// 1.Dat fiind un numar de la 1 la 12, returneaza numele lunii


asociate.
//am facut asa ca sa includ si numerele din afara intervalului (1,12)

// public static String month(int n) {


// String[] s = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December" };
// // declaring an array containing all the string representations
of the months
//
//
// if (n >= 1 && n <= 12) {
// return s[n - 1]; // The length of the array is n-1 (because
it starts with 0 )
//
// } else {
// return "Invalid month number";
// }
// }

//modalitatea cu SWITCH
public static String luna (int nr) {
String mesaj = "";

switch (nr) {
case 1:
mesaj = "Ianuarie";
break;
case 2:
mesaj = "Februarie";
break;
case 3:
mesaj = "Martie";
break;
case 4:
mesaj = "Aprilie";
break;
case 5:
mesaj = "Mai";
break;
case 6:
mesaj = "Iunie";
break;
case 7:
mesaj = "Iulie";
break;
case 8:
mesaj = "August";
break;
case 9:
mesaj = "Septembrie";
break;
case 10:
mesaj = "Octombrie";
break;
case 11:
mesaj = "Noiembrie";
break;
case 12:
mesaj = "Decembrie";
break;
default:
mesaj = "Numarul nu se incadreaza in interval";
}
return mesaj;

// modalitatea inversa a exercitiului anterior

// public static String month (int n) {


// String[] s = {"January",
"February","March","April","May","June","July","August","September","October","Nove
mber","December"};
//
// if (n<1 || n>12) {
// return "Invalid month number";
// }
// else {
// return s[n-1];
// }

}
}

You might also like