Programovanie (1) v C/C++
1-INF-127, ZS 2024/25
Letný semester, prednáška č. 1: Rozdiel medzi revíziami
Skočit na navigaci
Skočit na vyhledávání
Riadok 29: | Riadok 29: | ||
== Nič nové pod Slnkom... == | == Nič nové pod Slnkom... == | ||
+ | |||
+ | <syntaxhighlight lang="java">import java.util.*; | ||
+ | |||
+ | public class Program { | ||
+ | |||
+ | /* | ||
+ | Spocita sucet prvkov pola a. | ||
+ | */ | ||
+ | public static int sum(int[] a) { | ||
+ | int result = 0; | ||
+ | for (int i = 0; i <= a.length - 1; i++) { | ||
+ | result += a[i]; | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | Spocita priemer hodnot prvkov pola a. | ||
+ | */ | ||
+ | public static double average(int[] a) { | ||
+ | return (double) sum(a) / a.length; | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | Najde najvacsi prvok pola a. | ||
+ | */ | ||
+ | public static int max(int[] a) { | ||
+ | int max = Integer.MIN_VALUE; // Premenna ma rovnaky nazov ako metoda | ||
+ | for (int i = 0; i <= a.length - 1; i++) { | ||
+ | if (a[i] >= max) { | ||
+ | max = a[i]; | ||
+ | } | ||
+ | } | ||
+ | return max; | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | Scanner scanner = new Scanner(System.in); | ||
+ | |||
+ | int n = scanner.nextInt(); | ||
+ | int[] a = new int[n]; | ||
+ | |||
+ | for (int i = 0; i <= n - 1; i++) { | ||
+ | a[i] = scanner.nextInt(); | ||
+ | } | ||
+ | scanner.close(); | ||
+ | |||
+ | System.out.println("Sucet: " + sum(a)); | ||
+ | System.out.println("Priemer: " + average(a)); | ||
+ | System.out.println("Maximum: " + max(a)); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
== Základné konštrukcie jazyka Java == | == Základné konštrukcie jazyka Java == |
Verzia zo dňa a času 15:07, 6. február 2021
Obsah
Organizácia predmetu v letnom semestri
Základné črty jazyka Java
Prvý program v jazyku Java
Tradične začneme programom, ktorý na konzolu vypíše text Hello, World!.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
package hello;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Nič nové pod Slnkom...
import java.util.*;
public class Program {
/*
Spocita sucet prvkov pola a.
*/
public static int sum(int[] a) {
int result = 0;
for (int i = 0; i <= a.length - 1; i++) {
result += a[i];
}
return result;
}
/*
Spocita priemer hodnot prvkov pola a.
*/
public static double average(int[] a) {
return (double) sum(a) / a.length;
}
/*
Najde najvacsi prvok pola a.
*/
public static int max(int[] a) {
int max = Integer.MIN_VALUE; // Premenna ma rovnaky nazov ako metoda
for (int i = 0; i <= a.length - 1; i++) {
if (a[i] >= max) {
max = a[i];
}
}
return max;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i <= n - 1; i++) {
a[i] = scanner.nextInt();
}
scanner.close();
System.out.println("Sucet: " + sum(a));
System.out.println("Priemer: " + average(a));
System.out.println("Maximum: " + max(a));
}
}