Created
May 21, 2023 23:21
-
-
Save CodeByAidan/f06e86ca430e0a9b1fbad3525960a623 to your computer and use it in GitHub Desktop.
a lot of basic java methods condensed into a few files with simplicity!
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.*; | |
| interface MyInterface { | |
| void doSomething(); | |
| } | |
| enum MyEnum { | |
| FIRST, | |
| SECOND, | |
| THIRD | |
| } | |
| abstract class AbstractClass { | |
| abstract void doSomethingElse(); | |
| } | |
| class ParentClass { | |
| void commonMethod() { | |
| System.out.println("Parent class method"); | |
| } | |
| } | |
| class ChildClass extends ParentClass implements MyInterface { | |
| @Override | |
| public void doSomething() { | |
| System.out.println("Implemented method from interface"); | |
| } | |
| @Override | |
| public void commonMethod() { | |
| System.out.println("Overridden method from parent class"); | |
| } | |
| } | |
| class AnotherClass extends AbstractClass { | |
| @Override | |
| void doSomethingElse() { | |
| System.out.println("Implemented abstract method"); | |
| } | |
| } | |
| public class Main { | |
| public static void main(String[] args) { | |
| ChildClass childClass = new ChildClass(); | |
| childClass.doSomething(); | |
| childClass.commonMethod(); | |
| AnotherClass anotherClass = new AnotherClass(); | |
| anotherClass.doSomethingElse(); | |
| MyEnum first = MyEnum.FIRST; | |
| switch (first) { | |
| case FIRST: | |
| System.out.println("First enum value"); | |
| break; | |
| case SECOND: | |
| System.out.println("Second enum value"); | |
| break; | |
| case THIRD: | |
| System.out.println("Third enum value"); | |
| break; | |
| } | |
| } | |
| } |
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.*; | |
| import java.io.*; | |
| import java.nio.file.*; | |
| public class Example { | |
| public static void main(String[] args) { | |
| // String methods | |
| String str = "Hello, World!"; | |
| System.out.println(str.substring(0, 5)); | |
| System.out.println(str.toLowerCase()); | |
| System.out.println(str.charAt(7)); | |
| // File and I/O methods | |
| try { | |
| File file = new File("example.txt"); | |
| if (!file.exists()) { | |
| file.createNewFile(); | |
| } | |
| FileWriter writer = new FileWriter(file); | |
| writer.write(str); | |
| writer.close(); | |
| String content = new String(Files.readAllBytes(Paths.get("example.txt"))); | |
| System.out.println(content); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| // List and Collections methods | |
| List<String> list = new ArrayList<>(); | |
| list.add("Java"); | |
| list.add("Python"); | |
| list.add("C++"); | |
| System.out.println(list.get(1)); | |
| Collections.sort(list); | |
| for (String language : list) { | |
| System.out.println(language); | |
| } | |
| // Date methods | |
| Date date = new Date(); | |
| System.out.println(date.toString()); | |
| // Math methods | |
| System.out.println(Math.sqrt(16)); | |
| System.out.println(Math.max(10, 20)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment