Lab2 part 1

This commit is contained in:
Aelita4 2022-10-18 09:04:44 +02:00
commit b4d8939e29
Signed by: Aelita4
GPG Key ID: C217320B9C5FD53B
7 changed files with 271 additions and 0 deletions

29
.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/lab2.iml" filepath="$PROJECT_DIR$/lab2.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

78
.idea/workspace.xml Normal file
View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="49638fa3-c946-4bb6-9c97-1a148c116245" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/lab2.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Main.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2GIPSCMtHMWwcu8YkI6ihVwIoSe" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;project.structure.last.edited&quot;: &quot;Modules&quot;,
&quot;project.structure.proportion&quot;: &quot;0.0&quot;,
&quot;project.structure.side.proportion&quot;: &quot;0.0&quot;,
&quot;settings.editor.selected.configurable&quot;: &quot;preferences.pluginManager&quot;
}
}</component>
<component name="RunManager">
<configuration name="Main" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="Main" />
<module name="lab2" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="49638fa3-c946-4bb6-9c97-1a148c116245" name="Changes" comment="" />
<created>1666073128001</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1666073128001</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project>

11
lab2.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

133
src/Main.java Normal file
View File

@ -0,0 +1,133 @@
import java.util.Locale;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void z1() {
int n, a = 0, b = 10;
double suma = 0, pkt, ile = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("Podaj liczbę studentów: ");
n = scanner.nextInt();
if(n > 0) {
while(n > 0) {
System.out.print("Podaj ilość pkt (0-10):");
pkt = scanner.nextDouble();
if(pkt >= a && pkt <= b) {
suma += pkt;
ile++;
n--;
}
}
System.out.println("Ile: " + ile);
System.out.println("Suma: " + suma);
System.out.println("Średnia: " + (suma / ile));
} else {
System.out.println("Ilość studentów musi być liczbą > 0");
}
}
public static double z2_wczytaj() {
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();
return a;
}
public static void z2() {
double sumaUjemne = 0, sumaDodatnie = 0, ileUjemnych = 0, n;
for(int i = 1; i <= 10; i++) {
System.out.print("Podaj " + i + " liczbę: ");
n = z2_wczytaj();
if(n < 0) {
sumaUjemne += n;
ileUjemnych++;
} else {
sumaDodatnie += n;
}
}
System.out.println("Ujemnych: " + ileUjemnych);
System.out.println("Suma dodatnich: " + sumaDodatnie);
System.out.println("Suma ujemnych: " + sumaUjemne);
}
public static void z3() {
Scanner scanner = new Scanner(System.in);
int n;
System.out.print("Podaj n: ");
n = scanner.nextInt();
int x, sumaParzyste = 0;
for(int i = 1; i <= n; i++) {
System.out.print(i + ": ");
x = scanner.nextInt();
if(x % 2 == 0) {
sumaParzyste += x;
}
}
System.out.println("Suma parzyste: " + sumaParzyste);
}
public static void z4() {
Scanner scanner = new Scanner(System.in);
Random rand = new Random();
int a = -10, b = 45, x, sumaParzystych = 0;
System.out.print("Podaj n: ");
int n = scanner.nextInt();
for(int i = 1; i <= n; i++) {
x = rand.nextInt(b - a + 1) + a;
System.out.println(i + ". " + x);
if(x % 2 == 0) {
sumaParzystych += x;
}
}
System.out.println("Suma parzystych: " + sumaParzystych);
}
public static void z5() {
String s;
Scanner scanner = new Scanner(System.in);
s = scanner.next();
s.toLowerCase(Locale.ROOT);
boolean palindrom = true;
for(int i = 0; i < (s.length() / 2); i++) {
if(s.charAt(i) != s.charAt(s.length() - i - 1)) {
palindrom = false;
break;
}
}
System.out.println(palindrom ? "Palindrom" : "Nie palindrom");
}
public static void main(String[] args) {
System.out.println("=== Zadanie 1 (grupa lab) ===");
z1();
System.out.println("=== Zadanie 2 (10 liczb, suma) ===");
z2();
System.out.println("=== Zadanie 3 (ciąg parzystych) ===");
z3();
System.out.println("=== Zadanie 4 (losowanie, suma parzystych) ===");
z4();
System.out.println("=== Zadanie 5 (palindrom) ===");
z5();
}
}