├── MP2 ├── MP-G2 │ └── item1.java ├── MP2-G5 │ ├── Item4.class │ ├── Item6.class │ ├── Item13.class │ ├── Item2.java │ ├── Item1.java │ ├── Item12.java │ ├── Item11.java │ ├── Item6.java │ ├── Item10.java │ ├── Item15.java │ ├── Item8.java │ ├── Item3.java │ ├── Item9.java │ ├── Item7.java │ ├── Item5.java │ ├── Item4.java │ └── Item13.java ├── MP2-G8 │ ├── Item9.java │ ├── Item14.java │ ├── Item6.java │ ├── Item3.java │ ├── Item5.java │ ├── Item10.java │ ├── Item15.java │ ├── Item4.java │ ├── Item12.java │ ├── Item8.java │ ├── Item2.java │ ├── Item13.java │ ├── Item11.java │ └── Item1.java ├── group007-MP2 │ ├── Item9.java │ ├── Item2.java │ ├── Item1.java │ ├── Item8.java │ ├── Item6.java │ ├── Item3.java │ ├── Item12.java │ ├── Item5.java │ ├── Item11.java │ ├── Item4.java │ ├── Item15.java │ ├── Item10.java │ ├── Item7.java │ └── Item14.java ├── MP2-G6 │ ├── Item9.java │ ├── Item12.java │ ├── Item8.java │ ├── Item6.java │ ├── Item10.java │ ├── Item4.java │ ├── Item1.java │ ├── Item2.java │ ├── Item5.java │ ├── Item11.java │ └── Item7.java ├── MP2-G11 │ ├── Item12.java │ ├── Item9.java │ ├── Item1.java │ ├── Item2.java │ ├── Item5.java │ ├── Item6.java │ ├── Item8.java │ ├── Item3.java │ ├── Item4.java │ ├── Item7.java │ ├── Item11.java │ ├── Item15.java │ ├── Item10.java │ └── Item14.java ├── MP2_Grp02 │ ├── Item09 │ │ └── Number9.java │ ├── Item01 │ │ └── mp2_1.java │ ├── Item05 │ │ └── kaprekar.java │ ├── Item11 │ │ └── mp2_11.java │ ├── Item12 │ │ └── Item12.java │ ├── Item08 │ │ └── Item8.java │ ├── Item06 │ │ └── MP2NUM6.java │ ├── Item02 │ │ └── Item2.java │ ├── Item04 │ │ └── Item4.java │ ├── Item03 │ │ └── Number3.java │ ├── Item10 │ │ └── mp2_10.java │ ├── Item15 │ │ └── frequencyCounter.java │ └── Item14 │ │ └── transposeMatrix.java ├── MP2-G10 │ ├── Item9.java │ ├── Item11.java │ ├── Item12.java │ ├── Item10.java │ ├── Item5.java │ ├── Item1.java │ ├── Item8.java │ ├── Item3.java │ ├── Item7.java │ ├── Item15.java │ ├── Item4.java │ ├── Item6.java │ └── Item2.java ├── MP2-G3 │ ├── Item9.java │ ├── Item1.java │ ├── Item12.java │ ├── Item2.java │ ├── Item11.java │ ├── Item5.java │ ├── Item6.java │ ├── Item7.java │ ├── Item8.java │ ├── Item4.java │ ├── Item3.java │ ├── Item15.java │ └── Item10.java ├── MP-G9 │ ├── Item2.java │ ├── Item12.java │ ├── Item9.java │ ├── Item1.java │ ├── Item6.java │ ├── Item3.java │ ├── Item8.java │ ├── Item11.java │ ├── Item4.java │ ├── Item10.java │ ├── Item5.java │ ├── Item15.java │ ├── Item14.java │ └── Item7.java ├── MP-G4 │ ├── Item9.java │ ├── Item8.java │ ├── Item1.java │ ├── Item6.java │ ├── Item3.java │ ├── Item14.java │ ├── Item11.java │ ├── Item10.java │ ├── Item5.java │ ├── Item15.java │ └── Item12.java └── MP-G1 │ ├── Item9.java │ ├── Item12.java │ ├── Item1.java │ ├── Item15.java │ ├── Item8.java │ ├── Item2.java │ ├── Item4.java │ └── Item6.java ├── MP1 ├── Group03.class ├── Group08.java ├── Group02.java ├── Group09.java ├── Group07.java ├── Group10.java └── Group11.java ├── README.md ├── Java Basics Demo ├── PrecedenceDemo.java ├── ArraysDemo.java ├── StringsDemo.java ├── FunctionsDemo.java ├── VariablesDemo.java ├── InputDemo.java ├── OperatorsDemo.java └── ControlDemo.java ├── Lecture 4 Collection Constructors File Handling ├── File Handling │ ├── DeleteFileExample.java │ ├── WriteFileExample.java │ ├── CreateFileExample.java │ ├── AppendFileExample.java │ └── ReadFileExample.java ├── Constructors │ ├── ParameterizedConstructor.java │ ├── DefaultConstructor.java │ ├── ThisConstructor.java │ └── OverloadedConstructor.java └── Abstract vs Interfaces │ ├── InterfaceClass.java │ └── AbstractClass.java ├── Lecture 2 Codes Array String and Methods ├── ArrayExample.java ├── ArrayMethods.java └── MethodsDemo.java └── Lecture 3 OOP Concepts ├── Inheritance ├── BasicInheritance.java ├── MultiLevelInheritance.java └── HierarchicalInheritance.java ├── Polymorphism ├── RunTimePolymorphism.java └── CompileTimePolymorphism.java ├── Abstraction ├── InterfaceClass.java └── AbstractClass.java └── Encapsulation ├── BasedOnDataMethod.java ├── EncapsulationGetterSetter.java └── BasedOnAccessModifier.java /MP2/MP-G2/item1.java: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /MP1/Group03.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChrisPiamonte/OOP-CS25/HEAD/MP1/Group03.class -------------------------------------------------------------------------------- /MP2/MP2-G5/Item4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChrisPiamonte/OOP-CS25/HEAD/MP2/MP2-G5/Item4.class -------------------------------------------------------------------------------- /MP2/MP2-G5/Item6.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChrisPiamonte/OOP-CS25/HEAD/MP2/MP2-G5/Item6.class -------------------------------------------------------------------------------- /MP2/MP2-G5/Item13.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChrisPiamonte/OOP-CS25/HEAD/MP2/MP2-G5/Item13.class -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OOP-CS25N 2 | 3 | Hi! Welcome to the Object Oriented Programming Class. 4 | 5 | This will be our safe space for programming and coding. 6 | Let's learn new things and enjoy coding! -------------------------------------------------------------------------------- /Java Basics Demo/PrecedenceDemo.java: -------------------------------------------------------------------------------- 1 | public class PrecedenceDemo { 2 | public static void main(String[] args) { 3 | boolean boolResult = 5 + 3 * 2 > 10 && 4 != 3; 4 | System.out.println("Final Result: " + boolResult); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Java Basics Demo/ArraysDemo.java: -------------------------------------------------------------------------------- 1 | public class ArraysDemo { 2 | public static void main(String[] args) { 3 | int[] arrNumbers = {10, 20, 30, 40, 50}; 4 | 5 | System.out.println("First element: " + arrNumbers[0]); 6 | System.out.println("All elements:"); 7 | for (int intNumber : arrNumbers) { 8 | System.out.println(intNumber); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Java Basics Demo/StringsDemo.java: -------------------------------------------------------------------------------- 1 | public class StringsDemo { 2 | public static void main(String[] args) { 3 | String strWord = "Java Programming"; 4 | 5 | System.out.println("Length: " + strWord.length()); 6 | System.out.println("Uppercase: " + strWord.toUpperCase()); 7 | System.out.println("Character at index 5: " + strWord.charAt(5)); 8 | System.out.println("Substring (0-4): " + strWord.substring(0, 4)); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Lecture 4 Collection Constructors File Handling/File Handling/DeleteFileExample.java: -------------------------------------------------------------------------------- 1 | import java.io.File; 2 | 3 | public class DeleteFileExample { 4 | public static void main(String[] args) { 5 | File file = new File("example.txt"); 6 | if (file.delete()) { 7 | System.out.println("Deleted the file: " + file.getName()); 8 | } else { 9 | System.out.println("Failed to delete the file."); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Java Basics Demo/FunctionsDemo.java: -------------------------------------------------------------------------------- 1 | public class FunctionsDemo { 2 | // Function to add two numbers 3 | static int addNumbers(int intA, int intB) { 4 | return intA + intB; 5 | } 6 | 7 | // Function to greet 8 | static void greetPerson(String strName) { 9 | System.out.println("Hello, " + strName + "!"); 10 | } 11 | 12 | public static void main(String[] args) { 13 | System.out.println("Sum: " + addNumbers(3, 4)); 14 | greetPerson("Chris"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Lecture 4 Collection Constructors File Handling/Constructors/ParameterizedConstructor.java: -------------------------------------------------------------------------------- 1 | class Car { 2 | String brand; 3 | int year; 4 | 5 | // Parameterized constructor 6 | Car(String b, int y) { 7 | brand = b; 8 | year = y; 9 | } 10 | } 11 | 12 | public class ParameterizedConstructor { 13 | public static void main(String[] args) { 14 | Car car1 = new Car("Toyota", 2022); 15 | System.out.println("Brand: " + car1.brand + ", Year: " + car1.year); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item9.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ReverseString { 4 | public static void main(String[] args) { 5 | Scanner console = new Scanner(System.in); 6 | System.out.print("Enter a string: "); 7 | String str = console.nextLine(); 8 | 9 | String result = ""; 10 | 11 | for(int i = str.length()-1; i>=0; i--){ 12 | result += str.charAt(i); 13 | } 14 | 15 | System.out.println("Reversed string: " + result); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Lecture 2 Codes Array String and Methods/ArrayExample.java: -------------------------------------------------------------------------------- 1 | public class ArrayExample { 2 | public static void main(String[] args) { 3 | // Declare and initialize an array of integers 4 | int[] arrNumbers = {100, 200, 300, 400, 500}; 5 | 6 | // Display the elements of the array 7 | System.out.println("Array elements:"); 8 | 9 | for (int intCounter = 0; intCounter < arrNumbers.length; intCounter++) { 10 | System.out.println("Element at index " + intCounter + ": " + arrNumbers[intCounter]); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /Lecture 4 Collection Constructors File Handling/Constructors/DefaultConstructor.java: -------------------------------------------------------------------------------- 1 | class Car { 2 | String brand; 3 | int year; 4 | 5 | // Default constructor 6 | Car() { 7 | brand = "Unknown"; 8 | year = 2000; 9 | System.out.println("Default constructor called!"); 10 | } 11 | } 12 | 13 | public class DefaultConstructor { 14 | public static void main(String[] args) { 15 | Car car1 = new Car(); // calls the default constructor 16 | System.out.println("Brand: " + car1.brand + ", Year: " + car1.year); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /MP2/group007-MP2/Item9.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ReverseString 4 | { 5 | public static void main(String[] args) 6 | { 7 | Scanner sc = new Scanner (System.in); 8 | System.out.print("Enter a string: "); 9 | String string = sc.nextLine(); 10 | String reversed = ""; 11 | for (int i = string.length() - 1; i >= 0; i--) 12 | { 13 | reversed += string.charAt(i); 14 | } 15 | 16 | System.out.println("Reversed string: " + reversed); 17 | 18 | sc.close(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /MP2/MP2-G6/Item9.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item9 { 4 | public static void main(String[] args) { 5 | Scanner stringScanner = new Scanner(System.in); 6 | 7 | System.out.print("Enter your string: "); 8 | String stringInputted = stringScanner.nextLine(); 9 | stringScanner.close(); 10 | 11 | String resultString = ""; 12 | for (int i = 0; i < stringInputted.length(); i++) { 13 | resultString = stringInputted.charAt(i) + resultString; 14 | } 15 | System.out.println(resultString); 16 | } 17 | 18 | } 19 | 20 | -------------------------------------------------------------------------------- /MP2/MP2-G11/Item12.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item12 { 4 | public static void main(String[] args) { 5 | Scanner input = new Scanner(System.in); 6 | 7 | System.out.print("Enter a sentence: "); 8 | String sentence = input.nextLine(); 9 | 10 | String[] words = sentence.trim().split("\\s+"); 11 | 12 | if (sentence.trim().isEmpty()) { 13 | System.out.println("No Input"); 14 | } else { 15 | System.out.println("The sentence contains " + words.length + " words."); 16 | } 17 | 18 | input.close(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Lecture 4 Collection Constructors File Handling/Abstract vs Interfaces/InterfaceClass.java: -------------------------------------------------------------------------------- 1 | interface Flyable { 2 | void fly(); 3 | } 4 | 5 | interface Swimmable { 6 | void swim(); 7 | } 8 | 9 | class Duck implements Flyable, Swimmable { 10 | public void fly() { 11 | System.out.println("Duck is flying!"); 12 | } 13 | public void swim() { 14 | System.out.println("Duck is swimming!"); 15 | } 16 | } 17 | 18 | public class InterfaceClass { 19 | public static void main(String[] args) { 20 | Duck duck = new Duck(); 21 | duck.fly(); 22 | duck.swim(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MP2/MP2_Grp02/Item09/Number9.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Number9 { 4 | 5 | static void main(String[] args) { 6 | Scanner scanner = new Scanner(System.in); 7 | 8 | String reversedInput = ""; 9 | 10 | System.out.println("Enter Word here: "); 11 | String userInput = scanner.nextLine(); 12 | 13 | for (int i = userInput.length() - 1; i >= 0; i--) { 14 | 15 | reversedInput += userInput.charAt(i); 16 | } 17 | 18 | System.out.println("Orignal Word: " + userInput); 19 | System.out.println("Reverse Word: " + reversedInput); 20 | 21 | scanner.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /MP2/MP2-G6/Item12.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item12 { 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner input = new Scanner(System.in); 8 | 9 | System.out.print("Input a sentence: "); 10 | String sentence = input.nextLine(); 11 | 12 | if (sentence.trim().isEmpty()) { 13 | System.out.println("You did not enter any words!"); 14 | } else { 15 | String[] words = sentence.trim().split("\\s+"); 16 | System.out.println("Total words contained: " + words.length); 17 | } 18 | 19 | input.close(); 20 | } 21 | } -------------------------------------------------------------------------------- /Java Basics Demo/VariablesDemo.java: -------------------------------------------------------------------------------- 1 | public class VariablesDemo { 2 | public static void main(String[] args) { 3 | int intAge = 20; 4 | float fltPi = 3.141593f; 5 | double dblPrecisePi = 3.141592653589793; 6 | char charGrade = 'A'; 7 | String strName = "Chris"; 8 | boolean boolIsStudent = true; 9 | 10 | System.out.println("Age: " + intAge); 11 | System.out.println("Float Pi: " + fltPi); 12 | System.out.println("Double Pi: " + dblPrecisePi); 13 | System.out.println("Grade: " + charGrade); 14 | System.out.println("Name: " + strName); 15 | System.out.println("Is Student: " + boolIsStudent); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Lecture 4 Collection Constructors File Handling/Abstract vs Interfaces/AbstractClass.java: -------------------------------------------------------------------------------- 1 | abstract class Animal { 2 | // Abstract method (no implementation) 3 | abstract void sound(); 4 | 5 | // Concrete method (has implementation) 6 | void eat() { 7 | System.out.println("This animal eats food."); 8 | } 9 | } 10 | 11 | class Dog extends Animal { 12 | void sound() { 13 | System.out.println("Dog barks!"); 14 | } 15 | } 16 | 17 | public class AbstractClass { 18 | public static void main(String[] args) { 19 | Animal dog = new Dog(); // Upcasting 20 | dog.sound(); 21 | dog.eat(); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Lecture 4 Collection Constructors File Handling/File Handling/WriteFileExample.java: -------------------------------------------------------------------------------- 1 | import java.io.FileWriter; 2 | import java.io.IOException; 3 | 4 | public class WriteFileExample { 5 | public static void main(String[] args) { 6 | try { 7 | FileWriter writer = new FileWriter("example.txt"); 8 | writer.write("Hello, world!\nWelcome to Java File Handling."); 9 | writer.close(); 10 | System.out.println("Successfully wrote to the file."); 11 | } catch (IOException e) { 12 | System.out.println("An error occurred while writing."); 13 | e.printStackTrace(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /MP2/MP2-G10/Item9.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class Item9 { 4 | public static void main(String[] args) throws IOException { 5 | BufferedReader buffread = new BufferedReader(new InputStreamReader(System.in)); 6 | 7 | System.out.print("Enter String: "); 8 | String strInput = buffread.readLine(); 9 | int intStrLen = strInput.length(); 10 | 11 | char[] charArrOut = new char[intStrLen]; 12 | for (int intI = 0; intI < intStrLen; intI++) { 13 | charArrOut[intI] = strInput.charAt(intStrLen - intI - 1); 14 | } 15 | String strOut = new String(charArrOut); 16 | 17 | System.out.println("Reversed String: " + strOut); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /MP2/MP2-G11/Item9.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item9 { 4 | public static void stringReverse(String text){ 5 | char[] charArray = text.toCharArray(); 6 | 7 | System.out.print("Reversed String: "); 8 | for(int i = charArray.length - 1; i >= 0; i--){ 9 | System.out.print(charArray[i]); 10 | } 11 | } 12 | 13 | public static void main(String[] args) { 14 | 15 | Scanner sc = new Scanner(System.in); 16 | 17 | System.out.print("Enter String to be reversed: "); 18 | String strText = sc.nextLine(); 19 | 20 | stringReverse(strText); 21 | 22 | sc.close(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MP2/MP2_Grp02/Item01/mp2_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class mp2_1 { 4 | public static void main(String[] args) { 5 | Scanner input = new Scanner(System.in); 6 | int i = 0; 7 | int ctr = 0; 8 | 9 | System.out.print("Enter a number: "); 10 | int n = input.nextInt(); 11 | 12 | for (i = 1; i <= n/2; i++) { 13 | if(n % i == 0){ 14 | ctr += i; 15 | } 16 | } 17 | 18 | if(ctr == n){ 19 | System.out.println(n + " is a perfect number."); 20 | } else{ 21 | System.out.println(n + " is not a perfect number."); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Lecture 4 Collection Constructors File Handling/File Handling/CreateFileExample.java: -------------------------------------------------------------------------------- 1 | import java.io.File; 2 | import java.io.IOException; 3 | 4 | public class CreateFileExample { 5 | public static void main(String[] args) { 6 | try { 7 | File file = new File("example.txt"); 8 | if (file.createNewFile()) { 9 | System.out.println("File created: " + file.getName()); 10 | } else { 11 | System.out.println("File already exists."); 12 | } 13 | } catch (IOException e) { 14 | System.out.println("An error occurred."); 15 | e.printStackTrace(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Lecture 4 Collection Constructors File Handling/Constructors/ThisConstructor.java: -------------------------------------------------------------------------------- 1 | class Car { 2 | String brand; 3 | int year; 4 | 5 | // Constructor 1 6 | Car() { 7 | this("Unknown", 2000); // calls the second constructor 8 | System.out.println("Default constructor called!"); 9 | } 10 | 11 | // Constructor 2 12 | Car(String b, int y) { 13 | brand = b; 14 | year = y; 15 | } 16 | } 17 | 18 | public class ThisConstructor { 19 | public static void main(String[] args) { 20 | Car car1 = new Car(); // calls the first constructor 21 | System.out.println("Brand: " + car1.brand + ", Year: " + car1.year); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Lecture 4 Collection Constructors File Handling/File Handling/AppendFileExample.java: -------------------------------------------------------------------------------- 1 | import java.io.FileWriter; 2 | import java.io.IOException; 3 | 4 | public class AppendFileExample { 5 | public static void main(String[] args) { 6 | try { 7 | FileWriter writer = new FileWriter("example.txt", true); // true = append mode 8 | writer.write("\nThis line is appended to the file."); 9 | writer.close(); 10 | System.out.println("Successfully appended to the file."); 11 | } catch (IOException e) { 12 | System.out.println("An error occurred while appending."); 13 | e.printStackTrace(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /MP2/MP2-G5/Item2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item2 { 4 | public static void main(String args[]){ 5 | 6 | Scanner scanner = new Scanner(System.in); 7 | 8 | System.out.print("Input a number: "); 9 | int intInput = scanner.nextInt(); 10 | int intTemp = intInput; 11 | int intSumOfItsDigits = 0; 12 | scanner.close(); 13 | 14 | while(intTemp > 0){ 15 | int intModulo = intTemp % 10; 16 | intSumOfItsDigits += intModulo; 17 | intTemp /= 10; 18 | } 19 | 20 | if(intInput % intSumOfItsDigits == 0){ 21 | System.out.println(intInput + " is a Harshad number"); 22 | } 23 | else 24 | System.out.println(intInput + " is not a Harshad number"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /MP2/MP2-G11/Item1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item1 { 4 | public static void main(String[] args) { 5 | Scanner input = new Scanner(System.in); 6 | int num = 0; 7 | 8 | System.out.print("Enter a number: "); 9 | num = input.nextInt(); 10 | 11 | int sum = 0; 12 | for(int i = 1; i < num; i++){ 13 | if(num % i == 0){ 14 | sum += i; 15 | } 16 | } 17 | if(sum == num && num != 0){ 18 | System.out.println(num + " is a perfect number."); 19 | } else { 20 | System.out.println(num + " is not a perfect number."); 21 | } 22 | input.close(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MP2/MP2-G5/Item1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item1 { 4 | public static void main(String args[]){ 5 | 6 | Scanner scanner = new Scanner(System.in); 7 | 8 | System.out.print("Input Number: "); 9 | int intNumber = scanner.nextInt(); 10 | int intAddNumber = 0; 11 | scanner.close(); 12 | 13 | 14 | for(int i = 1; i < intNumber; i++){ 15 | if(intNumber % i == 0){ 16 | intAddNumber += i; 17 | } 18 | } 19 | 20 | 21 | if(intAddNumber == intNumber){ 22 | System.out.println(intNumber + " is a perfect number."); 23 | } 24 | else 25 | System.out.println(intNumber + " is not a perfect number."); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /MP1/Group08.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ========================================================== 3 | * Machine Problem 1 - Workspace for Group 08 4 | * ========================================================== 5 | * 6 | * Instructions: 7 | * - This file is your workspace for solving Machine Problem 1. 8 | * - You will practice the Programming Core Concepts discussed in class, 9 | * such as variables, data types, operators, and basic string operations. 10 | * - Do NOT create additional classes or advanced features (like arrays) 11 | * unless instructed. 12 | * ========================================================== 13 | */ 14 | 15 | public class Group08 { 16 | public static void main(String[] args) { 17 | 18 | } 19 | } -------------------------------------------------------------------------------- /MP2/MP2-G3/Item9.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item9 { 4 | public static void main(String[] args) { 5 | Scanner scan = new Scanner(System.in); 6 | System.out.print("Enter a string: "); 7 | 8 | //Ensure non-empty input 9 | String input = scan.nextLine(); 10 | while (input.trim().isEmpty()) { 11 | System.out.println("Invalid input! Please enter a non-empty string."); 12 | System.out.print("Enter a string: "); 13 | input = scan.nextLine(); 14 | } 15 | 16 | for (int i = input.length() - 1; i >= 0; i--) { 17 | System.out.print(input.charAt(i)); 18 | } 19 | scan.close(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MP2/MP2-G5/Item12.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item12 { 4 | 5 | public static void main(String[] args){ 6 | //Scanner initialization 7 | Scanner scanner = new Scanner(System.in); 8 | 9 | //Prompt the user to input a sentence 10 | System.out.println("Please enter a sentence:"); 11 | String sentence = scanner.nextLine(); 12 | 13 | //Counts the number of words by identifying the spaces 14 | int countWords = sentence.trim().split(" ").length; 15 | 16 | //Prints the countWords 17 | System.out.println("The number of words are:" + countWords); 18 | 19 | scanner.close(); //Close scanner 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Lecture 3 OOP Concepts/Inheritance/BasicInheritance.java: -------------------------------------------------------------------------------- 1 | // Parent class (Superclass) 2 | class Animal { 3 | public String name; 4 | 5 | void eat() { 6 | System.out.println("This animal eats food."); 7 | } 8 | 9 | void callName() { 10 | System.out.println("My pet is " + this.name); 11 | } 12 | } 13 | 14 | // Test class 15 | public class BasicInheritance { 16 | public static void main(String[] args) { 17 | Animal animal = new Animal(); 18 | animal.name = "Bogart"; // assigning value in Animal.name 19 | 20 | // Inherited method 21 | animal.callName(); // Output: My pet is Bogart 22 | animal.eat(); // Output: This animal eats food. 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Lecture 3 OOP Concepts/Inheritance/MultiLevelInheritance.java: -------------------------------------------------------------------------------- 1 | class Animal { 2 | void eat() { 3 | System.out.println("This animal eats food."); 4 | } 5 | } 6 | 7 | class Mammal extends Animal { 8 | void walk() { 9 | System.out.println("This mammal walks."); 10 | } 11 | } 12 | 13 | class Dog extends Mammal { 14 | void bark() { 15 | System.out.println("The dog barks."); 16 | } 17 | } 18 | 19 | public class MultiLevelInheritance { 20 | public static void main(String[] args) { 21 | Dog myDog = new Dog(); 22 | 23 | myDog.eat(); // Inherited from Animal 24 | myDog.walk(); // Inherited from Mammal 25 | myDog.bark(); // Defined in Dog 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /MP1/Group02.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ========================================================== 3 | * Machine Problem 1 - Workspace for Group 02 4 | * ========================================================== 5 | * 6 | * Instructions: 7 | * - This file is your workspace for solving Machine Problem 1. 8 | * - You will practice the Programming Core Concepts discussed in class, 9 | * such as variables, data types, operators, and basic string operations. 10 | * - Do NOT create additional classes or advanced features (like arrays) 11 | * unless instructed. 12 | * ========================================================== 13 | */ 14 | 15 | public class Group02 { 16 | public static void main(String[] args) { 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /MP2/MP2-G11/Item2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item2{ 4 | public static void main(String [] agrs){ 5 | Scanner input = new Scanner (System.in); 6 | int num = 0, sum = 0; 7 | 8 | 9 | System.out.print("Enter any number: "); 10 | num = input.nextInt(); 11 | int temp = num; 12 | 13 | while(temp > 0){ 14 | sum += temp % 10; 15 | temp /= 10; 16 | } 17 | 18 | if(sum != 0 && num % sum == 0){ 19 | System.out.println( num + " is a Hashad Number"); 20 | } else{ 21 | System.out.println( num + " is not a Hashad Number"); 22 | } 23 | input.close(); 24 | } 25 | } -------------------------------------------------------------------------------- /Java Basics Demo/InputDemo.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class InputDemo { 5 | public static void main(String[] args) throws IOException { 6 | // Using Scanner 7 | Scanner objScanner = new Scanner(System.in); 8 | System.out.print("Enter your age: "); 9 | int intAge = objScanner.nextInt(); 10 | System.out.println("You entered (Scanner): " + intAge); 11 | 12 | // Using BufferedReader 13 | BufferedReader objReader = new BufferedReader(new InputStreamReader(System.in)); 14 | System.out.print("Enter your name: "); 15 | String strName = objReader.readLine(); 16 | System.out.println("You entered (BufferedReader): " + strName); 17 | 18 | objScanner.close(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Lecture 4 Collection Constructors File Handling/File Handling/ReadFileExample.java: -------------------------------------------------------------------------------- 1 | import java.io.File; 2 | import java.io.FileNotFoundException; 3 | import java.util.Scanner; 4 | 5 | public class ReadFileExample { 6 | public static void main(String[] args) { 7 | try { 8 | File file = new File("example.txt"); 9 | Scanner reader = new Scanner(file); 10 | while (reader.hasNextLine()) { 11 | String line = reader.nextLine(); 12 | System.out.println(line); 13 | } 14 | reader.close(); 15 | } catch (FileNotFoundException e) { 16 | System.out.println("File not found."); 17 | e.printStackTrace(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /MP2/MP-G9/Item2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Item2 { 3 | public static void main(String[] args) { 4 | 5 | Scanner console = new Scanner(System.in); 6 | 7 | System.out.println("=== Harshad Number Classifier ==="); 8 | System.out.print("Enter a number: "); 9 | int n = console.nextInt(); 10 | 11 | int intSum = 0; int intTempN = n; 12 | 13 | while (intTempN > 0) { 14 | intSum += intTempN % 10; 15 | intTempN /= 10; 16 | } 17 | 18 | if (n % intSum == 0){ 19 | System.out.println(n + " is a Harshad Number"); 20 | } else { 21 | System.out.println(n + " is not a Harshad Number"); 22 | console.close(); 23 | 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /MP2/MP-G9/Item12.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item12 { 4 | public static int countWords(String strSentence) { 5 | if (strSentence == null || strSentence.trim().isEmpty()) { 6 | return 0; 7 | } 8 | String[] strWords = strSentence.trim().split("\\s+"); 9 | return strWords.length; 10 | } 11 | 12 | public static void main(String[] args) { 13 | Scanner scInput = new Scanner(System.in); 14 | 15 | System.out.print("Enter a sentence: "); 16 | String strSentence = scInput.nextLine(); 17 | 18 | int intWordCount = countWords(strSentence); 19 | System.out.println("Number of words: " + intWordCount); 20 | 21 | scInput.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /MP2/MP-G4/Item9.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item9 { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | 7 | System.out.print("Enter a string: "); 8 | String str = scanner.nextLine().trim(); 9 | 10 | System.out.println("Word: " + str); 11 | System.out.println("In reverse: " + reverse(str)); 12 | 13 | scanner.close(); 14 | } 15 | 16 | public static String reverse(String str) { 17 | char[] reverse = new char[str.length()]; 18 | 19 | for (int i = 0, j = str.length() - 1; i < str.length(); i++, j--) { 20 | reverse[i] = str.charAt(j); 21 | } 22 | 23 | return String.valueOf(reverse); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item14.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class TransposeOfAMatrix { 4 | public static void main(String[] args) { 5 | Scanner sx = new Scanner(System.in); 6 | int[][] matrix = new int[3][3]; 7 | 8 | System.out.println("Enter numbers for a matrix:"); 9 | for (int i = 0; i < 3; i++) { 10 | for (int j = 0; j < 3; j++) { 11 | matrix[i][j] = sx.nextInt(); 12 | } 13 | } 14 | System.out.println(" "); 15 | for (int i = 2; i >= 0; i--) { 16 | for (int j = 2; j >= 0; j--) { 17 | System.out.print(matrix[i][j] + " "); 18 | } 19 | System.out.println(); 20 | } 21 | sx.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Lecture 3 OOP Concepts/Inheritance/HierarchicalInheritance.java: -------------------------------------------------------------------------------- 1 | class Animal { 2 | void eat() { 3 | System.out.println("This animal eats food."); 4 | } 5 | } 6 | 7 | class Dog extends Animal { 8 | void bark() { 9 | System.out.println("The dog barks."); 10 | } 11 | } 12 | 13 | class Cat extends Animal { 14 | void meow() { 15 | System.out.println("The cat meows."); 16 | } 17 | } 18 | 19 | public class HierarchicalInheritance { 20 | public static void main(String[] args) { 21 | Dog myDog = new Dog(); 22 | Cat myCat = new Cat(); 23 | 24 | myDog.eat(); // Inherited from Animal 25 | myDog.bark(); 26 | 27 | myCat.eat(); // Inherited from Animal 28 | myCat.meow(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /MP2/MP2_Grp02/Item05/kaprekar.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class kaprekar { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter a number: "); 7 | int n = sc.nextInt(); 8 | 9 | int square = n*n; 10 | String str = String.valueOf(square); 11 | int len = str.length(); 12 | 13 | int rightDigits = (int) Math.pow(10, len/2); 14 | int leftPart = square / rightDigits; 15 | int rightPart = square % rightDigits; 16 | 17 | if (leftPart + rightPart == n) { 18 | System.out.println("The number is a kaprekar"); 19 | } else 20 | System.out.println("The number is not a kaprekar"); 21 | 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MP2/MP-G9/Item9.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Item9 { 3 | public static void main(String[] args){ 4 | Scanner scrInput = new Scanner(System.in); 5 | String strReverse = ""; 6 | System.out.print("Enter a string: "); 7 | String strInput = scrInput.nextLine(); 8 | while (strInput.equals("")) { 9 | System.out.println("No input detected try again!"); 10 | System.out.print("Enter a string: "); 11 | strInput = scrInput.nextLine(); 12 | } 13 | for (int i = strInput.length() - 1; i >= 0; i--) { 14 | strReverse += strInput.charAt(i); 15 | } 16 | 17 | System.out.println("Reversed string: " + strReverse); 18 | 19 | scrInput.close(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class PrimeFactors { 3 | public static void main(String[] args) { 4 | Scanner sx = new Scanner(System.in); 5 | System.out.print("Enter a number: "); 6 | int origNum = sx.nextInt(); 7 | System.out.print("Prime factors of " + origNum + ": "); 8 | while (origNum % 2 == 0) { 9 | System.out.print("2 "); 10 | origNum /= 2; 11 | } 12 | for (int i = 3; i * i <= origNum; i += 2) { 13 | while (origNum % i == 0) { 14 | System.out.print(i + " "); 15 | origNum /= i; 16 | } 17 | } 18 | if (origNum > 1) { 19 | System.out.print(origNum); 20 | } 21 | sx.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /MP2/MP2-G10/Item11.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item11 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter a string: "); 7 | String strInput = sc.nextLine(); 8 | sc.close(); 9 | 10 | int intVowels = 0, intConsonants = 0; 11 | for (int intI = 0; intI < strInput.length(); intI++) { 12 | char charTemp = strInput.charAt(intI); 13 | 14 | if (Character.isLetter(charTemp)) { 15 | char charTempLow = Character.toLowerCase(charTemp); 16 | if ("aeiou".indexOf(charTempLow) >= 0) intVowels++; 17 | else intConsonants++; 18 | } 19 | } 20 | 21 | System.out.println("Vowels: " + intVowels); 22 | System.out.println("Consonants: " + intConsonants); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MP2/MP2_Grp02/Item11/mp2_11.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class mp2_11 { 4 | public static void main(String[] args) { 5 | Scanner input = new Scanner (System.in); 6 | int vowels = 0; 7 | int consonants = 0; 8 | int whitespaces = 0; 9 | 10 | System.out.print("Enter a string: "); 11 | String line = input.nextLine(); 12 | line = line.toLowerCase(); 13 | 14 | for (int i = 0; i < line.length(); i++) { 15 | char ch = line.charAt(i); 16 | 17 | if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { 18 | vowels++; 19 | } else if (ch >= 'a' && ch <= 'z'){ 20 | consonants++; 21 | } 22 | } 23 | 24 | System.out.println("Vowels: " + vowels); 25 | System.out.println("Consonants: " + consonants); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /MP2/MP2_Grp02/Item12/Item12.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Item12 3 | { 4 | public static void main(String[] args) 5 | { 6 | //take user input 7 | String sentence; 8 | Scanner userInput = new Scanner(System.in); 9 | System.out.println("Please Input a Sentence: "); 10 | sentence = userInput.nextLine(); 11 | 12 | System.out.println("Word Count:"+ wordCounter(sentence)); 13 | 14 | 15 | userInput.close(); 16 | 17 | 18 | } 19 | //assumes that sentences are properly constructed 20 | //splits the sentence on space, then returns the length 21 | public static int wordCounter(String sentence) 22 | { 23 | String[] words; 24 | words = sentence.trim().split(" "); 25 | return words.length; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Java Basics Demo/OperatorsDemo.java: -------------------------------------------------------------------------------- 1 | public class OperatorsDemo { 2 | public static void main(String[] args) { 3 | int intA = 5, intB = 3; 4 | 5 | // Arithmetic 6 | System.out.println("a + b = " + (intA + intB)); 7 | System.out.println("a - b = " + (intA - intB)); 8 | System.out.println("a * b = " + (intA * intB)); 9 | System.out.println("a / b = " + (intA / intB)); 10 | System.out.println("a % b = " + (intA % intB)); 11 | 12 | // Relational 13 | System.out.println("a > b: " + (intA > intB)); 14 | System.out.println("a == b: " + (intA == intB)); 15 | 16 | // Logical 17 | boolean boolX = true, boolY = false; 18 | System.out.println("x && y: " + (boolX && boolY)); 19 | System.out.println("x || y: " + (boolX || boolY)); 20 | System.out.println("!x: " + (!boolX)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /MP2/MP2-G11/Item5.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item5 { 4 | public static void main(String[] args){ 5 | Scanner scanner = new Scanner(System.in); 6 | System.out.println("Enter a number to check if it's a Kaprekar Number:"); 7 | int number = scanner.nextInt(); 8 | 9 | if (isKaprekar(number)) { 10 | System.out.println(number + " is a Kaprekar number."); 11 | } else { 12 | System.out.println(number + " is not a Kaprekar number."); 13 | } 14 | scanner.close(); 15 | } 16 | public static boolean isKaprekar(int number) { 17 | int square = number * number; 18 | int digits = String.valueOf(number).length(); 19 | 20 | int right = square % (int)Math.pow(10, digits); 21 | int left = square / (int)Math.pow(10, digits); 22 | 23 | return left + right == number; 24 | } 25 | } -------------------------------------------------------------------------------- /MP2/MP2_Grp02/Item08/Item8.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Item8 { 4 | public static void main(String[] args){ 5 | 6 | Scanner sc = new Scanner(System.in); 7 | 8 | int modulus = 10; 9 | int result = 0; 10 | 11 | System.out.println("AUTOMORPHIC NUMBER CHECKER!\n"); 12 | System.out.println("Enter a number:"); 13 | int num = sc.nextInt(); 14 | 15 | int numSquared = num * num; 16 | 17 | while (result < num){ 18 | result = numSquared % modulus; 19 | modulus *= 10; 20 | } 21 | 22 | if (result == num){ 23 | System.out.println(num + " is an automorphic number!"); 24 | } 25 | else{ 26 | System.out.println(num + " is not an automorphic number!"); 27 | } 28 | 29 | sc.close(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /MP2/MP2_Grp02/Item06/MP2NUM6.java: -------------------------------------------------------------------------------- 1 | // NEO AMIDO 2 | // Prime Factors of a Digit 3 | 4 | import java.util.Scanner; 5 | 6 | public class MP2NUM6 { 7 | public static void main(String[] args) { 8 | Scanner scanner = new Scanner(System.in); 9 | int n; 10 | 11 | System.out.print("Enter Digit: "); 12 | n = scanner.nextInt(); 13 | scanner.nextLine(); 14 | 15 | System.out.print("Prime Factors of " + n + " are "); 16 | 17 | for (int i = 2; i <= Math.sqrt(n); i++) { 18 | while (n % i == 0) { 19 | System.out.print(i + " "); 20 | n /= i; 21 | } 22 | } 23 | 24 | if (n > 1) { 25 | System.out.print(n + " "); 26 | } 27 | 28 | System.out.println(); 29 | scanner.close(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /MP2/MP-G9/Item1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item1 { 4 | public static void main(String[] args) { 5 | 6 | Scanner console = new Scanner (System.in); 7 | 8 | System.out.println("=== Perfect Number Classifier ==="); 9 | System.out.print("Enter a number: "); 10 | int intNumber = console.nextInt(); 11 | int intSum = 0; 12 | for (int i = 1; i < intNumber; i++){ 13 | if (intNumber % i == 0) 14 | { 15 | intSum += i; 16 | } 17 | } 18 | if (intSum == intNumber){ 19 | System.out.print(intNumber + " is a perfect number!"); 20 | } else { 21 | System.out.println(intNumber + " is not a perfect number!"); 22 | } 23 | 24 | console.close(); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /MP2/MP2-G11/Item6.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item6 { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | System.out.println("Enter a number to find it's prime factors:"); 7 | int number = scanner.nextInt(); 8 | 9 | printPrimeFactors(number); 10 | 11 | scanner.close(); 12 | } 13 | public static void printPrimeFactors(int number){ 14 | while (number % 2 == 0){ 15 | System.out.print(2 + " "); 16 | number /= 2; 17 | } 18 | for (int i = 3; i <= Math.sqrt(number); i += 2){ 19 | while (number % i == 0){ 20 | System.out.print(i + " "); 21 | number /= i; 22 | } 23 | } if (number > 2){ 24 | System.out.print(number); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /MP2/MP-G1/Item9.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item9{ 4 | public static void main(String[] args){ 5 | Scanner scanner = new Scanner(System.in);//Scanner function 6 | //Ask a user to input a string to reverse 7 | System.out.print("Please enter a string to reverse: "); 8 | String origString = scanner.nextLine(); 9 | scanner.close(); 10 | 11 | String reverseString = ""; 12 | 13 | //Last character to the first character 14 | for(int i = origString.length() - 1; i >= 0; i--){ 15 | //Append to the reversed string 16 | reverseString += origString.charAt(i); 17 | } 18 | //Show the original and the reversed string 19 | System.out.println("The original string is " + origString); 20 | System.out.println("The reversed version of string is " + reverseString); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /MP2/MP2-G10/Item12.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item12 { 4 | public static void main (String[] args) { 5 | Scanner sc = new Scanner (System.in); 6 | System.out.print("Please enter a sentence: "); 7 | String strSentence = sc.nextLine(); 8 | sc.close(); 9 | 10 | strSentence = strSentence.trim(); 11 | int intWordCounter = 0; 12 | boolean boolSpaceChecker = false; 13 | 14 | for (int i = 0; i < strSentence.length(); i++) { 15 | char charFirstLetter = strSentence.charAt(i); 16 | 17 | if (charFirstLetter != ' ' && !boolSpaceChecker) { 18 | boolSpaceChecker = true; 19 | intWordCounter++; 20 | } 21 | else if (charFirstLetter == ' ') { 22 | boolSpaceChecker = false; 23 | } 24 | } 25 | 26 | System.out.print("The sentence " + strSentence); 27 | System.out.printf(" has %d words", intWordCounter); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /MP2/MP2-G10/Item10.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item10 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | System.out.print("Enter first string: "); 8 | String str1 = sc.nextLine(); 9 | 10 | System.out.print("Enter second string: "); 11 | String str2 = sc.nextLine(); 12 | sc.close(); 13 | 14 | String strNormal1 = str1.toLowerCase(); 15 | String strNormal2 = str2.toLowerCase(); 16 | 17 | char[] charArr1 = strNormal1.toCharArray(); 18 | char[] charArr2 = strNormal2.toCharArray(); 19 | 20 | Arrays.sort(charArr1); 21 | Arrays.sort(charArr2); 22 | 23 | if (Arrays.equals(charArr1, charArr2)) { 24 | System.out.printf("The strings %s and %s are anagrams\n", str1, str2); 25 | } 26 | else { 27 | System.out.printf("The strings %s and %s are not anagrams\n", str1, str2); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /MP2/MP2-G6/Item8.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item8 { 4 | 5 | public static void main(String[] args) { 6 | Scanner input = new Scanner(System.in); 7 | 8 | System.out.print("Enter a number: "); 9 | 10 | if (input.hasNextInt()) { 11 | int number = input.nextInt(); 12 | 13 | int square = number * number; 14 | 15 | String numStr = String.valueOf(number); 16 | String squareStr = String.valueOf(square); 17 | 18 | if (squareStr.endsWith(numStr)) { 19 | System.out.println(number + " is an Automorphic Number."); 20 | } else { 21 | System.out.println(number + " is NOT an Automorphic Number."); 22 | } 23 | 24 | } else { 25 | System.out.println("Invalid input! Please enter a whole number."); 26 | } 27 | 28 | input.close(); 29 | } 30 | } -------------------------------------------------------------------------------- /MP2/MP2-G11/Item8.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item8{ 4 | 5 | public static boolean strConversion(int num, int sqr) { 6 | 7 | String strNum = String.valueOf(num); 8 | String strSqr = String.valueOf(sqr); 9 | 10 | if(strSqr.endsWith(strNum)){ 11 | return true; 12 | } 13 | return false; 14 | 15 | } 16 | 17 | public static void main(String[] args){ 18 | 19 | Scanner sc = new Scanner(System.in); 20 | 21 | System.out.print("Enter a number to check: "); 22 | int intNum = sc.nextInt(); 23 | 24 | int intSquare = intNum * intNum; 25 | 26 | if(strConversion(intNum, intSquare)){ 27 | System.out.println(intNum + " is an Automorphic Number"); 28 | } else { 29 | System.out.println(intNum + " is not an Automorphic Number"); 30 | } 31 | sc.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Lecture 3 OOP Concepts/Polymorphism/RunTimePolymorphism.java: -------------------------------------------------------------------------------- 1 | // Parent class 2 | class Animal { 3 | public void makeSound() { 4 | System.out.println("Some generic animal sound"); 5 | } 6 | } 7 | 8 | // Child class Dog overrides makeSound 9 | class Dog extends Animal { 10 | @Override 11 | public void makeSound() { 12 | System.out.println("Woof Woof"); 13 | } 14 | } 15 | 16 | // Child class Cat overrides makeSound 17 | class Cat extends Animal { 18 | @Override 19 | public void makeSound() { 20 | System.out.println("Meow Meow"); 21 | } 22 | } 23 | 24 | public class RunTimePolymorphism { 25 | public static void main(String[] args) { 26 | Animal a; // Reference variable of type Animal 27 | 28 | a = new Dog(); 29 | a.makeSound(); // Calls Dog's version 30 | 31 | a = new Cat(); 32 | a.makeSound(); // Calls Cat's version 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Lecture 3 OOP Concepts/Polymorphism/CompileTimePolymorphism.java: -------------------------------------------------------------------------------- 1 | class Calculator { 2 | // Method with 2 integer parameters 3 | public int add(int a, int b) { 4 | return a + b; 5 | } 6 | 7 | // Method with 3 integer parameters 8 | public int add(int a, int b, int c) { 9 | return a + b + c; 10 | } 11 | 12 | // Method with 2 double parameters 13 | public double add(double a, double b) { 14 | return a + b; 15 | } 16 | } 17 | 18 | public class CompileTimePolymorphism { 19 | public static void main(String[] args) { 20 | Calculator calc = new Calculator(); 21 | 22 | System.out.println("Add two integers: " + calc.add(5, 10)); // Calls first method 23 | System.out.println("Add three integers: " + calc.add(1, 2, 3)); // Calls second method 24 | System.out.println("Add two doubles: " + calc.add(2.5, 3.5)); // Calls third method 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /MP2/MP2-G10/Item5.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item5 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | int intNum = 0; 8 | while (true) { 9 | System.out.print("Input a number: "); 10 | 11 | if (sc.hasNextInt()) { 12 | intNum = sc.nextInt(); 13 | 14 | if (intNum > 0) { 15 | break; 16 | } 17 | } 18 | sc.next(); 19 | } 20 | sc.close(); 21 | 22 | long longSquare = (long) Math.pow(intNum, 2); 23 | int intDigitCount = String.valueOf(intNum).length(); 24 | long long10PowOfNum = (long) Math.pow(10, intDigitCount); 25 | long longRight = longSquare % long10PowOfNum; 26 | long longLeft = longSquare / long10PowOfNum; 27 | 28 | if (longLeft + longRight == intNum) System.out.println(intNum + " is a Kaprekar number"); 29 | else System.out.println(intNum + " is not a Kaprekar number."); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class StrongNumber{ 4 | 5 | 6 | public static int factorial(int n) { 7 | int fact = 1; 8 | for (int i = 1; i <= n; i++) { 9 | fact *= i; 10 | } 11 | return fact; 12 | } 13 | 14 | public static void main(String[] args) { 15 | Scanner console = new Scanner(System.in); 16 | System.out.print("Enter a number: "); 17 | int num = console.nextInt(); 18 | 19 | int sum = 0; 20 | int number = num; 21 | 22 | while (number > 0) { 23 | int digit = number % 10; 24 | sum += factorial(digit); 25 | number /= 10; 26 | } 27 | 28 | 29 | if (sum == num) 30 | System.out.println(num + " is a Strong Number."); 31 | else 32 | System.out.println(num + " is not a Strong Number."); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /MP2/MP-G4/Item8.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item8 { 4 | public static void main(String[] args) { 5 | Scanner scan = new Scanner(System.in); 6 | int n; 7 | 8 | // Ask user for a positive number input 9 | while (true) { 10 | System.out.print("Enter a number: "); 11 | n = scan.nextInt(); 12 | 13 | if (n > 0) { 14 | break; 15 | } 16 | 17 | System.out.println("Invalid Input. Must be positive number."); 18 | } 19 | 20 | int square = n * n; 21 | int digits = String.valueOf(n).length(); 22 | 23 | int mod = (int) Math.pow(10, digits); 24 | if (square % mod == n) { 25 | System.out.println(n + " is an Automorphic Number."); 26 | } else { 27 | System.out.println(n + " is not an Automorphic Number."); 28 | } 29 | 30 | scan.close(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /MP2/MP2-G5/Item11.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item11 { 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner in = new Scanner(System.in); 8 | 9 | System.out.print("Enter String: "); 10 | 11 | String str1 = in.nextLine().toLowerCase(); 12 | 13 | int vowels = 0; 14 | int consonants = 0; 15 | 16 | for(int i = 0; i < str1.length(); i++){ 17 | char ch = str1.charAt(i); 18 | 19 | if(ch >= 'a' && ch <= 'z'){ 20 | if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { 21 | vowels++; 22 | } 23 | else{ 24 | consonants++; 25 | } 26 | } 27 | } 28 | 29 | System.out.println("Vowels: " +vowels); 30 | System.out.println("Consonants: " +consonants); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /MP2/MP2-G5/Item6.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class Item6{ 5 | public static void main (String args[]){ 6 | Scanner scanner = new Scanner (System.in); 7 | System.out.println("Enter a number: "); 8 | int myNumber = Integer.parseInt(scanner.nextLine()); 9 | ArrayList primeFactors = getPrimeFactors(myNumber); 10 | System.out.println("The prime factors of " + myNumber + " are " + primeFactors); 11 | scanner.close(); 12 | } 13 | 14 | public static ArrayList getPrimeFactors(int myNumber){ 15 | ArrayList factors = new ArrayList <> (); 16 | int i = 2; 17 | while (myNumber > 1){ 18 | if (myNumber % i == 0){ 19 | factors.add(i); 20 | myNumber /= i; 21 | } else { 22 | i++; 23 | } 24 | } 25 | return factors; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /MP2/MP2_Grp02/Item02/Item2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Item2 3 | { 4 | public static void main(String[] args) 5 | { 6 | //Asks user for input 7 | Scanner userInput = new Scanner(System.in); 8 | System.out.println("Enter a number: "); 9 | String[] userNumber = userInput.nextLine().split(""); 10 | 11 | 12 | System.out.println(harshad(userNumber)); 13 | 14 | 15 | userInput.close(); 16 | } 17 | 18 | //calculates user input 19 | //returns true if harshad, false if not 20 | public static boolean harshad(String[] userNumber) 21 | { 22 | 23 | int number; 24 | int sum = 0; 25 | 26 | number = Integer.parseInt(String.join("",userNumber)); 27 | for(int i = 0; i < userNumber.length; i++) 28 | { 29 | sum += Integer.parseInt(userNumber[i]); 30 | } 31 | return number % sum == 0; 32 | } 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /MP2/MP-G9/Item6.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | // Prime Factors of a Number 3 | 4 | public class Item6 { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | System.out.print("Enter a number: "); 10 | int intNumber = sc.nextInt(); 11 | 12 | System.out.print("The prime factors of ["+intNumber+"] are: "); 13 | 14 | 15 | while (intNumber % 2 == 0) { 16 | System.out.print(2 + " "); 17 | intNumber = intNumber / 2; 18 | } 19 | 20 | for (int intFactor = 3; intFactor <= Math.sqrt(intNumber); intFactor += 2) { 21 | while (intNumber % intFactor == 0) { 22 | System.out.print(intFactor + " "); 23 | intNumber = intNumber / intFactor; 24 | } 25 | } 26 | 27 | if (intNumber > 2) { 28 | System.out.print(intNumber); 29 | } 30 | 31 | sc.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item5.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class KaprekarNumber { 4 | public static void main(String[] args) { 5 | Scanner sx = new Scanner(System.in); 6 | System.out.print("Enter a number: "); 7 | int origNum = sx.nextInt(); 8 | 9 | int square = origNum * origNum; 10 | int divide = 10; 11 | int kaprekar = 0; 12 | 13 | while (divide <= square) { 14 | int left = square / divide; 15 | int right = square % divide; 16 | 17 | if (left + right == origNum && right != 0) { 18 | kaprekar++; 19 | break; 20 | } 21 | divide *= 10; 22 | } 23 | if (kaprekar == 1) { 24 | System.out.println(origNum + " is a Kaprekar number."); 25 | } else { 26 | System.out.println(origNum + " is NOT a Kaprekar number."); 27 | } 28 | sx.close(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /MP2/MP2-G5/Item10.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | public class Item10 { 5 | 6 | public static void main(String[] args){ 7 | 8 | Scanner in = new Scanner(System.in); 9 | 10 | System.out.print("Input two strings: "); 11 | 12 | String str1 = in.nextLine().toLowerCase().replaceAll("\\s", ""); 13 | String str2 = in.nextLine().toLowerCase().replaceAll("\\s", ""); 14 | 15 | char arr1[] = str1.toCharArray(); 16 | char arr2[] = str2.toCharArray(); 17 | 18 | Arrays.sort(arr1); 19 | Arrays.sort(arr2); 20 | 21 | String sortedstr1 = new String(arr1); 22 | String sortedstr2 = new String(arr2); 23 | 24 | if(sortedstr1.equals(sortedstr2)) { 25 | System.out.println("Anagram"); 26 | } 27 | else { 28 | System.out.println("not an Anagram"); 29 | } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /MP2/MP2-G5/Item15.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item15 { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | 7 | System.out.print("Input array Size: "); 8 | int intSize = scanner.nextInt(); 9 | 10 | 11 | int[] arrNumbers = new int[intSize]; 12 | 13 | for(int i = 0; i < intSize; i++){ 14 | System.out.print("Input element " + i + ": "); 15 | arrNumbers[i] = scanner.nextInt(); 16 | } 17 | scanner.close(); 18 | 19 | int intFrequency = 0; 20 | 21 | for(int i = 0; i < intSize; i++){ 22 | for(int j = 0; j 0){ 9 | result += Math.pow(num % 10, 2); 10 | num /= 10; 11 | } 12 | return result; 13 | } 14 | public static void main(String[] args){ 15 | 16 | Scanner sc = new Scanner(System.in); 17 | 18 | System.out.println("HAPPY NUMBER CHECKER!\n"); 19 | System.out.println("Please enter a number:"); 20 | int num = sc.nextInt(); 21 | 22 | int result = num; 23 | 24 | while (result != 1 && result != 4){ 25 | result = happyNumChecker(result); 26 | } 27 | 28 | if(result == 1){ 29 | System.out.println(num + " is a Happy number."); 30 | } 31 | else { 32 | System.out.println(num + " is not a Happy number."); 33 | } 34 | sc.close(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /MP2/group007-MP2/Item2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class HarshadNumber 4 | { 5 | public static boolean isHarshadNumber(int num) 6 | { 7 | if (num <= 0) 8 | { 9 | return false; 10 | } 11 | 12 | int sum = 0; 13 | int temp = num; 14 | while (temp > 0) 15 | { 16 | sum += temp % 10; 17 | temp /= 10; 18 | } 19 | return num % sum == 0; 20 | } 21 | 22 | public static void main(String[] args) 23 | { 24 | Scanner sc = new Scanner(System.in); 25 | System.out.print("Enter a number: "); 26 | int number = sc.nextInt(); 27 | sc.close(); 28 | 29 | if (isHarshadNumber(number)) 30 | { 31 | System.out.println(number + " is a Harshad number."); 32 | } 33 | else 34 | { 35 | System.out.println(number + " is not a Harshad number."); 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item10.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Anagram { 4 | 5 | public static void main(String[] args) { 6 | Scanner console = new Scanner(System.in); 7 | 8 | System.out.print("Enter String 1: "); 9 | String str1 = console.nextLine().toLowerCase(); 10 | 11 | System.out.print("Enter String 2: "); 12 | String str2 = console.nextLine().toLowerCase(); 13 | 14 | char[] array1 = str1.toCharArray(); 15 | char[] array2 = str2.toCharArray(); 16 | 17 | if(array1.length != array2.length){ 18 | System.out.println("Different lengths, not Anagrams"); 19 | return; 20 | } 21 | 22 | Arrays.sort(array1); 23 | Arrays.sort(array2); 24 | 25 | if(Arrays.equals(array1, array2)){ 26 | System.out.print("The string are Anagrams."); 27 | }else{ 28 | System.out.print("The strings are not Anagrams."); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /MP2/MP-G9/Item3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Item3 { 3 | public static void main(String[] args) { 4 | 5 | Scanner console = new Scanner(System.in); 6 | 7 | System.out.println("=== Strong Number Checker === "); 8 | System.out.print("Please enter a number: "); 9 | int intNumber = console.nextInt(); 10 | 11 | int tempN = intNumber; int sum = 0; 12 | while (tempN > 0){ 13 | int lastDigit = tempN % 10; // get last digit 14 | long longFact = 1; 15 | for (int i = 1; i <= lastDigit; i++) { 16 | longFact *= i; // factorial of last digit 17 | sum += longFact; // get the sum 18 | tempN /= 10; // remove last digit then proceed to the next iteration 19 | } 20 | } 21 | 22 | if (intNumber == sum){ 23 | System.out.println(intNumber + " is a STRONG NUMBER"); 24 | } else { 25 | System.out.println(intNumber + " is NOT a STRONG NUMBER"); 26 | } 27 | console.close(); 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /MP2/group007-MP2/Item1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class PerfectNumber 4 | { 5 | public static boolean isPerfectNumber(int num) 6 | { 7 | if (num <= 1) 8 | { 9 | return false; 10 | } 11 | 12 | int sum = 0; 13 | for (int i = 1; i <= num / 2; i++) 14 | { 15 | if (num % i == 0) 16 | { 17 | sum += i; 18 | } 19 | } 20 | return sum == num; 21 | } 22 | 23 | public static void main(String[] args) 24 | { 25 | Scanner sc = new Scanner(System.in); 26 | System.out.print("Enter a number: "); 27 | int number = sc.nextInt(); 28 | sc.close(); 29 | 30 | if (isPerfectNumber(number)) 31 | { 32 | System.out.println(number + " is a perfect number."); 33 | } 34 | else 35 | { 36 | System.out.println(number + " is not a perfect number."); 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Lecture 4 Collection Constructors File Handling/Constructors/OverloadedConstructor.java: -------------------------------------------------------------------------------- 1 | class Car { 2 | String brand; 3 | int year; 4 | 5 | // Default constructor 6 | Car() { 7 | brand = "Unknown"; 8 | year = 2000; 9 | } 10 | 11 | // Constructor with one parameter 12 | Car(String b) { 13 | brand = b; 14 | year = 2023; 15 | } 16 | 17 | // Constructor with two parameters 18 | Car(String b, int y) { 19 | brand = b; 20 | year = y; 21 | } 22 | } 23 | 24 | public class OverloadedConstructor { 25 | public static void main(String[] args) { 26 | Car car1 = new Car(); // default 27 | Car car2 = new Car("Honda"); // one parameter 28 | Car car3 = new Car("Ford", 2025); // two parameters 29 | 30 | System.out.println(car1.brand + " - " + car1.year); 31 | System.out.println(car2.brand + " - " + car2.year); 32 | System.out.println(car3.brand + " - " + car3.year); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /MP2/MP-G4/Item1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item1 { 4 | public static void main(String[] args) { 5 | Scanner scan = new Scanner(System.in); 6 | int number; 7 | 8 | // Ask user for a positive number input 9 | while (true) { 10 | System.out.print("Enter a number: "); 11 | number = scan.nextInt(); 12 | 13 | if (number > 0) { 14 | break; 15 | } 16 | 17 | System.out.println("Invalid Input. Must be positive number."); 18 | } 19 | 20 | int sum = 0; 21 | for (int i = number / 2; i > 0; i--) { 22 | if (number % i == 0) { 23 | sum += i; 24 | } 25 | } 26 | 27 | if (sum == number) { 28 | System.out.println(number + " is a Perfect Number."); 29 | } else { 30 | System.out.println(number + " is not a Perfect Number."); 31 | } 32 | 33 | scan.close(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /MP2/MP2-G11/Item3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item3 { 4 | public static int factorial(int num){ 5 | int result = 1; 6 | for(int i = 1; i <= num; i++){ 7 | result *= i; 8 | } 9 | return result; 10 | } 11 | 12 | 13 | public static void main(String[] args){ 14 | Scanner input = new Scanner(System.in); 15 | 16 | System.out.println("Enter a number: "); 17 | int num = input.nextInt(); 18 | int temp = num; 19 | int digit = 0; 20 | int sum = 0; 21 | 22 | while (temp > 0){ 23 | digit = temp % 10; 24 | sum += factorial(digit); 25 | temp /= 10; 26 | 27 | } 28 | 29 | if(sum != 0 && sum == num){ 30 | System.out.println(num + " is a Strong Number"); 31 | } else{ 32 | System.out.println(num + " is not a Strong Number"); 33 | } 34 | 35 | 36 | input.close(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /MP2/MP2-G5/Item3.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item3 { 4 | 5 | 6 | public static int factorial(int n) { 7 | int fact =1; 8 | for(int i=1; i<=n; i++){ 9 | fact*=i; 10 | } 11 | return fact; 12 | } 13 | 14 | public static boolean strongNum(int number) { 15 | int original = number; 16 | int sum = 0; 17 | 18 | while(number>0) { 19 | int digit = number%10; 20 | sum+= factorial(digit); 21 | number/=10; 22 | } 23 | return sum==original; 24 | } 25 | public static void main(String[] args) { 26 | Scanner sc = new Scanner(System.in); 27 | 28 | System.out.print("Enter a number: "); 29 | int n = sc.nextInt(); 30 | 31 | if(strongNum(n)) { 32 | System.out.println(n+ " is a strong number"); 33 | } 34 | else { 35 | System.out.println(n + " is not a strong number"); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /MP2/MP2-G5/Item9.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item9{ 4 | 5 | public static void main(String[] args) { 6 | //Scanner Declaration 7 | Scanner scanner = new Scanner(System.in); 8 | 9 | //Prompt the user to enter a string 10 | System.out.println("Enter a string"); 11 | String myString = scanner.nextLine(); 12 | 13 | //Initialize String variable to hold the reversed String 14 | String reversedString = ""; 15 | 16 | //for loop to reverse the myString variable without built-in functions 17 | for(int i = myString.length() - 1;i >= 0;i--){ 18 | char letter = myString.charAt(i); 19 | reversedString = reversedString + letter; 20 | } 21 | 22 | //printing the myString and reversedString 23 | System.out.println("Original String: " + myString); 24 | System.out.println("Reversed String: " + reversedString); 25 | 26 | scanner.close(); //Close Scanner 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item15.java: -------------------------------------------------------------------------------- 1 | public class ElementFrequency { 2 | public static void main(String[] args) { 3 | int[] numbers = {2, 4, 4, 4, 6, 6, 6, 6, 6}; // Example array 4 | int n = numbers.length; 5 | int[] frequency = new int[n]; 6 | boolean[] counted = new boolean[n]; 7 | 8 | for (int i = 0; i < n; i++) { 9 | if (numCount[i]) continue; // Skip if already counted 10 | frequency[i] = 1; 11 | 12 | for (int j = i + 1; j < n; j++) { 13 | if (numbers[i] == numbers[j]) { 14 | frequency[i]++; 15 | numCount[j] = true; 16 | } 17 | } 18 | numCount[i] = true; // Mark the element as counted 19 | } 20 | 21 | System.out.println("Element | Frequency"); 22 | for (int i = 0; i < n; i++) { 23 | if frequency[i] != 0 { 24 | System.out.println(numbers[i] + "appeared " + frequency[i] + " times."); 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /MP2/MP-G4/Item6.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item6 { 4 | public static void main(String args[]){ 5 | // init int, scanner inst 6 | int inputNumber = 0; 7 | Scanner console = new Scanner(System.in); 8 | 9 | System.out.println("This program is defined for all integers"); 10 | System.out.println("Input an integer to prime factorize: "); 11 | 12 | if ((inputNumber = console.nextInt()) < 0) inputNumber += inputNumber * -2; 13 | 14 | System.out.println("Here are the prime factors of " + inputNumber); 15 | primeFactorize(inputNumber, 2); 16 | } 17 | 18 | public static void primeFactorize(int number, int factor){ 19 | if (number == 1) return; 20 | 21 | if (number % factor == 0) { // if divisible by current divisor 22 | System.out.println(factor); 23 | primeFactorize(number / factor, factor); 24 | } else { // increase divisor 25 | primeFactorize(number, factor + 1); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Lecture 3 OOP Concepts/Abstraction/InterfaceClass.java: -------------------------------------------------------------------------------- 1 | // Interface 2 | interface Vehicle { 3 | void start(); // abstract by default 4 | void stop(); 5 | } 6 | 7 | // Implementing class: Car 8 | class Car implements Vehicle { 9 | @Override 10 | public void start() { 11 | System.out.println("Car starting..."); 12 | } 13 | 14 | @Override 15 | public void stop() { 16 | System.out.println("Car stopped."); 17 | } 18 | } 19 | 20 | // Implementing class: Bike 21 | class Bike implements Vehicle { 22 | @Override 23 | public void start() { 24 | System.out.println("Bike starting..."); 25 | } 26 | 27 | @Override 28 | public void stop() { 29 | System.out.println("Bike stopped."); 30 | } 31 | } 32 | 33 | // Main class 34 | public class InterfaceClass { 35 | public static void main(String[] args) { 36 | Vehicle car = new Car(); 37 | car.start(); 38 | car.stop(); 39 | 40 | Vehicle bike = new Bike(); 41 | bike.start(); 42 | bike.stop(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /MP2/MP2-G5/Item7.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class TwinPrimesInRange { 4 | public static void main(String[] args) { 5 | Scanner scan = new Scanner(System.in); 6 | 7 | // Ask user for range 8 | System.out.print("Enter starting number: "); 9 | int start = scan.nextInt(); 10 | 11 | System.out.print("Enter ending number: "); 12 | int end = scan.nextInt(); 13 | 14 | System.out.println("Twin primes between " + start + " and " + end + ":"); 15 | 16 | // Find twin primes 17 | for (int i = start; i <= end - 2; i++) { 18 | if (isPrime(i) && isPrime(i + 2)) { 19 | System.out.println("(" + i + ", " + (i + 2) + ")"); 20 | } 21 | } 22 | 23 | scan.close(); 24 | } 25 | 26 | // Function to check prime number 27 | public static boolean isPrime(int num) { 28 | if (num <= 1) return false; 29 | for (int i = 2; i * i <= num; i++) { 30 | if (num % i == 0) return false; 31 | } 32 | return true; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /MP2/MP2_Grp02/Item03/Number3.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class Number3 { 5 | public static int factorial(int n) { 6 | int factorial = 1; 7 | for (int i = 1; i <= n; i++) { 8 | factorial *= i; 9 | } 10 | return factorial; 11 | 12 | } 13 | 14 | public static boolean isStrongNumber(int num) { 15 | 16 | int originalNum = num; 17 | int sum = 0; 18 | 19 | while (num > 0) { 20 | 21 | int digit = num % 10; 22 | sum += factorial(digit); 23 | num /= 10; 24 | } 25 | 26 | return sum == originalNum; 27 | 28 | } 29 | 30 | public static void main(String[] args) { 31 | 32 | Scanner scanner = new Scanner(System.in); 33 | 34 | System.out.println("Enter Number here: "); 35 | int userNumber = scanner.nextInt(); 36 | 37 | if (isStrongNumber(userNumber)) { 38 | 39 | System.out.println(userNumber + " is a Strong number."); 40 | 41 | } else { 42 | 43 | System.out.println((userNumber + " is not a Strong Number")); 44 | 45 | } 46 | 47 | scanner.close(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /MP1/Group09.java: -------------------------------------------------------------------------------- 1 | public class Group09{ 2 | public static void main(String[] args) { 3 | int intInputNumber = 12321; 4 | System.out.println("Number " + intInputNumber + 5 | (checkPalindrome(intInputNumber) ? " is" : " is not") + " a Palindrome"); 6 | 7 | System.out.println("Palindromic numbers from 1-500:"); 8 | for (int intLoopNumber = 1; intLoopNumber <= 500; intLoopNumber++) { 9 | if (checkPalindrome(intLoopNumber)) { 10 | System.out.print(intLoopNumber + " "); 11 | } 12 | } 13 | } 14 | public static boolean checkPalindrome(int intNum) { 15 | int intOriginal = intNum; 16 | int intReverse = 0; 17 | while (intNum != 0) { 18 | int intDigit = intNum % 10; 19 | int intSum = 0; 20 | for (int i = 0; i < 10; i++) { 21 | intSum += intReverse; 22 | } 23 | intReverse = intSum + intDigit; 24 | intNum = intNum / 10; 25 | } 26 | return intOriginal == intReverse; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /MP2/MP-G9/Item8.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item8 { 4 | public static void main(String[] args) { 5 | Scanner scrInput = new Scanner(System.in); 6 | 7 | System.out.print("Enter a number: "); 8 | while (!scrInput.hasNextInt()) { 9 | System.out.println("Invalid input! Please enter an integer."); 10 | scrInput.next(); 11 | System.out.print("Enter a number: "); 12 | } 13 | int number = scrInput.nextInt(); 14 | 15 | System.out.println(checkAutomorphic(number) ? "is an automorphic number" : "is not an automorphic number"); 16 | 17 | scrInput.close(); 18 | } 19 | 20 | public static boolean checkAutomorphic(int intNumber) { 21 | int intSquare = intNumber * intNumber; 22 | while (intNumber != 0) { 23 | if (intNumber % 10 != intSquare % 10) { 24 | return false; 25 | } 26 | intNumber = intNumber / 10; 27 | intSquare = intSquare / 10; 28 | } 29 | return true; 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Lecture 3 OOP Concepts/Abstraction/AbstractClass.java: -------------------------------------------------------------------------------- 1 | // Abstract class 2 | abstract class Animal { 3 | // Abstract method (must be implemented by subclasses) 4 | public abstract void sound(); 5 | 6 | // Concrete method (shared implementation) 7 | public void sleep() { 8 | System.out.println("Sleeping..."); 9 | } 10 | } 11 | 12 | // Subclass Dog implements the abstract method 13 | class Dog extends Animal { 14 | @Override 15 | public void sound() { 16 | System.out.println("Woof Woof"); 17 | } 18 | } 19 | 20 | // Subclass Cat implements the abstract method 21 | class Cat extends Animal { 22 | @Override 23 | public void sound() { 24 | System.out.println("Meow Meow"); 25 | } 26 | } 27 | 28 | // Main class 29 | public class AbstractClass { 30 | public static void main(String[] args) { 31 | Animal dog = new Dog(); 32 | dog.sound(); // Calls Dog's version 33 | dog.sleep(); // Inherited method 34 | 35 | Animal cat = new Cat(); 36 | cat.sound(); // Calls Cat's version 37 | cat.sleep(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item4.java: -------------------------------------------------------------------------------- 1 | public class HappyNumber { 2 | public static void main(String[] args) { 3 | int number = 7; // Replace with the number you want to check 4 | if (isHappyNumber(number)) { 5 | System.out.println(number + " is a Happy Number."); 6 | } else { 7 | System.out.println(number + " is not a Happy Number."); 8 | } 9 | } 10 | // Uses Floyd's Cycle-Finding Algorithm 11 | public static boolean isHappyNumber(int num) { 12 | int slow = num; 13 | int fast = num; 14 | 15 | do { 16 | slow = squareSum(slow); // Move slow by one step 17 | fast = squareSum(squaresum(fast)); // Move fast by two steps 18 | } while (slow != fast); 19 | 20 | return slow == 1; // If they meet at 1, it's a happy number 21 | } 22 | 23 | public static int squareSum(int num) { 24 | int sum = 0; 25 | while (num > 0) { 26 | int lastDigit = num % 10; 27 | sum += lastDigit * lastDigit; 28 | num /= 10; 29 | } 30 | return sum; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /MP2/MP-G9/Item11.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item11 { 4 | public static void countVowelsAndConsonants(String strText) { 5 | int intVowelCount = 0; 6 | int intConsonantCount = 0; 7 | String strVowels = "aeiou"; 8 | strText = strText.toLowerCase(); 9 | for (int i = 0; i < strText.length(); i++) { 10 | char chrCurrent = strText.charAt(i); 11 | if (Character.isLetter(chrCurrent)) { 12 | if (strVowels.indexOf(chrCurrent) != -1) { 13 | intVowelCount++; 14 | } else { 15 | intConsonantCount++; 16 | } 17 | } 18 | } 19 | System.out.println("Vowels: " + intVowelCount); 20 | System.out.println("Consonants: " + intConsonantCount); 21 | } 22 | public static void main(String[] args) { 23 | Scanner scInput = new Scanner(System.in); 24 | System.out.print("Enter a string: "); 25 | String strInput = scInput.nextLine(); 26 | countVowelsAndConsonants(strInput); 27 | scInput.close(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /MP2/MP2-G11/Item4.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item4 { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | System.out.print("Enter a number: "); 7 | int number = scanner.nextInt(); 8 | 9 | if (isHappyNumber(number)) { 10 | System.out.println(number + " is a happy number."); 11 | } else { 12 | System.out.println(number + " is not a happy number."); 13 | } 14 | scanner.close(); 15 | } 16 | 17 | public static boolean isHappyNumber(int number) { 18 | int temp = number; 19 | 20 | while (true) { 21 | int sum = 0; 22 | 23 | while (temp > 0) { 24 | int digit = temp % 10; 25 | sum += digit * digit; 26 | temp /= 10; 27 | } 28 | 29 | if (sum == 1) 30 | return true; 31 | if (sum == 4) 32 | return false; 33 | 34 | temp = sum; 35 | } 36 | } 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /MP2/MP2-G11/Item7.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item7 { 4 | public static boolean isPrime(int num) { 5 | if(num <= 1){ 6 | return false; 7 | } 8 | for(int i = 2; i <= Math.sqrt(num); i++){ 9 | if (num % i == 0){ 10 | return false; 11 | } 12 | } 13 | return true; 14 | } 15 | 16 | public static void twinPrimes(int start, int end){ 17 | for(int i = start; i < end; i++){ 18 | if(isPrime(i) && isPrime(i + 2)){ 19 | System.out.printf("(%d, %d)", i, i+2); 20 | } 21 | } 22 | } 23 | 24 | public static void main(String[] args) { 25 | 26 | Scanner sc = new Scanner(System.in); 27 | 28 | System.out.print("Enter minimum range: "); 29 | int intMin = sc.nextInt(); 30 | 31 | System.out.print("Enter maximum range: "); 32 | int intMax = sc.nextInt(); 33 | 34 | System.out.printf("Twin primes between %d and %d are: \n", intMin, intMax); 35 | twinPrimes(intMin, intMax); 36 | 37 | sc.close(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /MP2/MP-G4/Item3.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item3 { 4 | public static void main(String[] args) { 5 | Scanner scan = new Scanner(System.in); 6 | int number; 7 | 8 | // Ask user for a positive number input 9 | while (true) { 10 | System.out.print("Enter a number: "); 11 | number = scan.nextInt(); 12 | 13 | if (number > 0) { 14 | break; 15 | } 16 | 17 | System.out.println("Invalid Input. Must be positive number."); 18 | } 19 | 20 | int temp = number, sum = 0; 21 | while (temp > 0) { 22 | int digit = temp % 10; 23 | int fact = 1; 24 | for (int i = 1; i <= digit; i++) 25 | fact *= i; 26 | sum += fact; 27 | temp /= 10; 28 | } 29 | 30 | if (sum == number) { 31 | System.out.println(number + " is a Strong Number."); 32 | } else { 33 | System.out.println(number + " is not a Strong Number."); 34 | } 35 | 36 | scan.close(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /MP2/MP2-G10/Item1.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class Item1 { 4 | public static void main(String[] args) throws IOException { 5 | BufferedReader buffread = new BufferedReader(new InputStreamReader(System.in)); 6 | 7 | int intInputNum = 0; 8 | while (true) { 9 | System.out.print("Number: "); 10 | 11 | try { 12 | intInputNum = Integer.parseInt(buffread.readLine().trim()); 13 | } 14 | catch (NumberFormatException e) { 15 | System.out.println("Please input valid integer"); 16 | } 17 | 18 | if (intInputNum >= 1) { 19 | break; 20 | } 21 | } 22 | 23 | if (isPerfectNumber(intInputNum)) { 24 | System.out.printf("%d is a perfect Number.\n", intInputNum); 25 | } 26 | else { 27 | System.out.printf("%d is not a perfect Number.\n", intInputNum); 28 | } 29 | } 30 | 31 | public static boolean isPerfectNumber(int intN) { 32 | int intSum = 0; 33 | for (int intI = 1; intI <= intN / 2; intI++) { 34 | if (intN % intI == 0) { 35 | intSum += intI; 36 | } 37 | } 38 | 39 | return intSum == intN; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /MP2/MP2-G3/Item1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item1 4 | { 5 | static Scanner console = new Scanner(System.in); 6 | public static void main (String[] args) 7 | { 8 | int divideSum = 0; 9 | System.out.println("Input a number"); 10 | if (!console.hasNextInt()) 11 | { 12 | System.out.println("Invalid input! Please enter an integer."); 13 | return; 14 | } 15 | int inputNum = console.nextInt(); 16 | System.out.println("You input " + inputNum + "!"); 17 | for(int i = 1; i<= inputNum-1; i++) 18 | { 19 | if(inputNum % i == 0) 20 | { 21 | divideSum = divideSum + i; 22 | } 23 | } 24 | if(divideSum == inputNum) 25 | { 26 | System.out.println("The number you input, " + inputNum + ", is a Perfect number"); 27 | } 28 | else 29 | { 30 | System.out.println("The number you input, " + inputNum + ", is NOT a Perfect number"); 31 | } 32 | return; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Java Basics Demo/ControlDemo.java: -------------------------------------------------------------------------------- 1 | public class ControlDemo { 2 | public static void main(String[] args) { 3 | int intNumber = 7; 4 | 5 | // if-else 6 | if (intNumber % 2 == 0) { 7 | System.out.println(intNumber + " is even"); 8 | } else { 9 | System.out.println(intNumber + " is odd"); 10 | } 11 | 12 | // switch 13 | int intDay = 3; 14 | switch (intDay) { 15 | case 1: System.out.println("Monday"); break; 16 | case 2: System.out.println("Tuesday"); break; 17 | case 3: System.out.println("Wednesday"); break; 18 | default: System.out.println("Other Day"); 19 | } 20 | 21 | // Loops 22 | System.out.println("For loop:"); 23 | for (int intI = 1; intI <= 5; intI++) { 24 | System.out.print(intI + " "); 25 | } 26 | 27 | System.out.println("\nWhile loop:"); 28 | int intJ = 1; 29 | while (intJ <= 5) { 30 | System.out.print(intJ + " "); 31 | intJ++; 32 | } 33 | 34 | System.out.println("\nDo-While loop:"); 35 | int intK = 1; 36 | do { 37 | System.out.print(intK + " "); 38 | intK++; 39 | } while (intK <= 5); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /MP2/group007-MP2/Item8.java: -------------------------------------------------------------------------------- 1 | import java.util.InputMismatchException; 2 | import java.util.Scanner; 3 | 4 | public class AutomorphicNumber { 5 | public static void main(String[] args) { 6 | Scanner input = new Scanner(System.in); 7 | System.out.print("Input a number to check if its an Automorphic Number: "); 8 | try { 9 | int number = input.nextInt(); 10 | int square = number*number; 11 | String strSquare = Integer.toString(square); 12 | System.out.println("The square of " + number + " is " + square); 13 | String strNumber = Integer.toString(number); 14 | if(strSquare.endsWith(strNumber)){ 15 | System.out.println("This number is a Automorphic Number"); 16 | }else{ 17 | System.out.println("This number is not a Automorphic Number"); 18 | } 19 | } catch (InputMismatchException e) { 20 | System.out.println("Wrong input!!"); 21 | } catch (Exception e) { 22 | System.out.println("An error occurred: " + e.getMessage()); 23 | } 24 | input.close(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /MP2/MP2-G11/Item11.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item11 { 4 | 5 | public static void main(String[] args) { 6 | Scanner scanner = new Scanner(System.in); 7 | 8 | System.out.print("Enter Word: "); 9 | String input = scanner.nextLine(); 10 | 11 | String lowerCaseInput = input.toLowerCase(); 12 | 13 | int vowelCount = 0; 14 | int consonantCount = 0; 15 | 16 | for (int i = 0; i < lowerCaseInput.length(); i++) { 17 | char ch = lowerCaseInput.charAt(i); 18 | 19 | if (Character.isLetter(ch)) { 20 | if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { 21 | vowelCount++; 22 | } else { 23 | consonantCount++; 24 | } 25 | } 26 | } 27 | 28 | System.out.println("\nOriginal string: " + input); 29 | System.out.println("Vowels: " + vowelCount); 30 | System.out.println("Consonants: " + consonantCount); 31 | System.out.println("Total letters: " + (vowelCount + consonantCount)); 32 | 33 | scanner.close(); 34 | } 35 | } -------------------------------------------------------------------------------- /MP2/MP2-G3/Item12.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 12. Word Count in a Sentence 3 | * Ask the user to input a sentence and count how many words it contains. 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | public class Item12 { 9 | public static void main(String[] args) { 10 | 11 | Scanner scanner = new Scanner(System.in); 12 | String stringUserInput = ""; 13 | 14 | // Loop until the user inputs something 15 | while(true){ 16 | System.out.println("Enter a sentence: "); 17 | stringUserInput = scanner.nextLine(); 18 | 19 | // Remove unnecessary spaces at the start/end 20 | stringUserInput.trim(); 21 | 22 | // Exits loop if input is valid 23 | if (!stringUserInput.isEmpty()){ 24 | break; 25 | } 26 | 27 | System.out.println("Please input a sentence. Try Again."); 28 | } 29 | 30 | // Split into words via spaces then count them 31 | String[] words = stringUserInput.split("\\s+"); 32 | System.out.println("Word count: " + words.length); 33 | 34 | // close scanner 35 | scanner.close(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /MP2/MP2_Grp02/Item10/mp2_10.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class mp2_10 { 4 | 5 | public static boolean areAnagrams(String str1, String str2) { 6 | 7 | char[] arr1 = str1.toCharArray(); 8 | char[] arr2 = str2.toCharArray(); 9 | Arrays.sort(arr1); 10 | Arrays.sort(arr2); 11 | 12 | return Arrays.equals(arr1, arr2); 13 | } 14 | 15 | public static void main(String[] args) { 16 | Scanner input = new Scanner (System.in); 17 | 18 | System.out.print("Enter the first string: "); 19 | String str1 = input.nextLine(); 20 | String concatenatedStr1 = str1.replaceAll("\\s", ""); 21 | 22 | System.out.print("Enter the second string: "); 23 | String str2 = input.nextLine(); 24 | String concatenatedStr2 = str2.replaceAll("\\s", ""); 25 | 26 | 27 | if(areAnagrams(concatenatedStr1, concatenatedStr2) == true){ 28 | System.out.println(str1 + " and " + str2 + " are anagrams."); 29 | } 30 | else{ 31 | System.out.println(str1 + " and " + str2 + " are not anagrams."); 32 | } 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /MP2/MP2-G6/Item6.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item6 { 4 | 5 | public static void main(String[] args) { 6 | Scanner input = new Scanner(System.in); 7 | 8 | System.out.print("Enter a number: "); 9 | 10 | if (input.hasNextInt()) { 11 | int number = input.nextInt(); 12 | 13 | if (number <= 0) { 14 | System.out.println("Please enter a positive number greater than 0."); 15 | } else { 16 | System.out.println("Prime factors of " + number + " are:"); 17 | 18 | while (number % 2 == 0) { 19 | System.out.print(2 + " "); 20 | number = number / 2; 21 | } 22 | 23 | for (int i = 3; i <= number; i += 2) { 24 | while (number % i == 0) { 25 | System.out.print(i + " "); 26 | number = number / i; 27 | } 28 | } 29 | } 30 | } else { 31 | System.out.println("Invalid input! Please enter a whole number."); 32 | } 33 | 34 | input.close(); 35 | } 36 | } -------------------------------------------------------------------------------- /MP2/MP2-G11/Item15.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Item15 { 3 | public static void main(String[] args) { 4 | Scanner console = new Scanner(System.in); 5 | 6 | System.out.print("Enter the number of elements in the array: "); 7 | int intNum = console.nextInt(); 8 | int[] arr = new int[intNum]; 9 | boolean[] counted = new boolean[intNum]; 10 | 11 | for (int i = 0; i < intNum; i++) { 12 | System.out.print("Enter element #" + (i + 1) + ": "); 13 | arr[i] = console.nextInt(); 14 | counted[i] = false; 15 | } 16 | 17 | System.out.println("\nFrequency of elements:"); 18 | for (int i = 0; i < intNum; i++) { 19 | if (!counted[i]) { 20 | int count = 1; 21 | for (int j = i + 1; j < intNum; j++) { 22 | if (arr[i] == arr[j]) { 23 | count++; 24 | counted[j] = true; 25 | } 26 | } 27 | System.out.println(arr[i] + " occurs " + count + " time(s)"); 28 | } 29 | } 30 | console.close(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /MP2/MP2-G3/Item2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item2 4 | { 5 | static Scanner console = new Scanner(System.in); 6 | public static void main (String[] args) 7 | { 8 | int trackedNum; 9 | int digitSum = 0; 10 | System.out.println("Input a number"); 11 | if (!console.hasNextInt()) 12 | { 13 | System.out.println("Invalid input! Please enter an integer."); 14 | return; 15 | } 16 | int inputNum = console.nextInt(); 17 | trackedNum = inputNum; 18 | System.out.println("You input " + inputNum + "!"); 19 | while(trackedNum > 0) 20 | { 21 | int lDigit = trackedNum%10; 22 | digitSum = digitSum + lDigit; 23 | trackedNum = trackedNum/10; 24 | } 25 | if(inputNum % digitSum == 0) 26 | { 27 | System.out.println("Your input, " + inputNum + ", is a Harshad number"); 28 | } 29 | else 30 | { 31 | System.out.println("Your input, " + inputNum + ", is NOT a Harshad number"); 32 | } 33 | return; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /MP2/MP2-G10/Item8.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item8 { 4 | public static int square (int intNum) { 5 | int intSquared = intNum * intNum; 6 | 7 | return intSquared; 8 | } 9 | 10 | public static boolean automorphic (int intN) { 11 | int intTemp = intN; 12 | int intCounter = 0; 13 | while (intTemp > 0) { 14 | intTemp /= 10; 15 | intCounter++; 16 | } 17 | 18 | int intLastDigit = square(intN) % (int) Math.pow (10, intCounter); 19 | 20 | return intLastDigit == intN; 21 | } 22 | 23 | public static void main (String[] args) { 24 | Scanner sc = new Scanner (System.in); 25 | 26 | int intNumber = 0; 27 | while (true) { 28 | System.out.print("Please enter a number: "); 29 | 30 | if (sc.hasNextInt()) { 31 | intNumber = sc.nextInt(); 32 | 33 | if (intNumber > 0) { 34 | break; 35 | } 36 | } 37 | sc.next(); 38 | } 39 | 40 | if (automorphic(intNumber)) { 41 | System.out.println(intNumber + " is an Automorphic Number"); 42 | } 43 | else { 44 | System.out.println(intNumber + " is not an Automorphic Number"); 45 | } 46 | 47 | sc.close(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item12.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | 4 | public class Item12{ 5 | public static void main(String[] args){ 6 | Scanner sc = new Scanner(System.in); 7 | 8 | System.out.println("Please enter a sentence: "); 9 | String sentence = sc.nextLine().trim();//removes the leading/ending white spaces 10 | sc.close(); 11 | 12 | int wordCount = 0; 13 | boolean inside = false;//checks if iteration is in a word 14 | 15 | for(int i=0; i < sentence.length(); i++){ 16 | char c = sentence.charAt(i);//gets the character at index (iteration) 17 | if(Character.isLetterOrDigit(c)){//checks if the current char is a letter or number 18 | if(!inside){ 19 | wordCount++; 20 | inside = true; 21 | }//increments wordcount if it reads a char at the beginning and after a white space 22 | } 23 | else{ 24 | inside = false; 25 | }//resets the inside if we are in a white space 26 | 27 | } 28 | 29 | System.out.println("There are " + wordCount + " word(s) in the sentence."); 30 | } 31 | } -------------------------------------------------------------------------------- /MP2/MP2-G11/Item10.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class Item10 { 5 | public static void main(String[] args) { 6 | Scanner input = new Scanner(System.in); 7 | 8 | System.out.print("Enter string: "); 9 | String strFirst = input.nextLine(); 10 | 11 | System.out.print("Enter string: "); 12 | String strSecond = input.nextLine(); 13 | 14 | if (anagramCheck(strFirst, strSecond)) { 15 | System.out.println("The strings are anagrams of each other."); 16 | } else { 17 | System.out.println("The strings are NOT anagrams of each other."); 18 | } 19 | 20 | input.close(); 21 | } 22 | 23 | public static boolean anagramCheck(String s1, String s2) { 24 | s1 = s1.replaceAll("\\s", "").toLowerCase(); 25 | s2 = s2.replaceAll("\\s", "").toLowerCase(); 26 | 27 | if (s1.length() != s2.length()) { 28 | return false; 29 | } 30 | 31 | char[] arr1 = s1.toCharArray(); 32 | char[] arr2 = s2.toCharArray(); 33 | 34 | Arrays.sort(arr1); 35 | Arrays.sort(arr2); 36 | 37 | return Arrays.equals(arr1, arr2); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /MP2/MP2_Grp02/Item15/frequencyCounter.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class frequencyCounter { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | System.out.print("Enter number of elements: "); 8 | int n = sc.nextInt(); 9 | 10 | int[] arr = new int[n]; 11 | int[] freq = new int[n]; 12 | int visited = -1; 13 | 14 | System.out.println("Enter elements:"); 15 | for (int i = 0; i < n; i++) { 16 | arr[i] = sc.nextInt(); 17 | } 18 | 19 | for (int i = 0; i < n; i++) { 20 | int count = 1; 21 | for (int j = i + 1; j < n; j++) { 22 | if (arr[i] == arr[j]) { 23 | count++; 24 | freq[j] = visited; 25 | } 26 | } 27 | if (freq[i] != visited) 28 | freq[i] = count; 29 | } 30 | 31 | System.out.println("\nElement | Frequency"); 32 | for (int i = 0; i < n; i++) { 33 | if (freq[i] != visited) 34 | System.out.println(" " + arr[i] + " | " + freq[i]); 35 | } 36 | 37 | sc.close(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /MP2/MP-G9/Item4.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | // Happy Number 3 | 4 | public class Item4 { 5 | static int intSquares(int intNumber) { 6 | int intSum = 0, intDigit; 7 | while (intNumber > 0) { 8 | intDigit = intNumber % 10; 9 | intSum += intDigit * intDigit; 10 | intNumber /= 10; 11 | } 12 | return intSum; 13 | } 14 | 15 | static boolean boolHappyNumber(int intNumber) { 16 | int intCurrent = intNumber, intNext = intNumber; 17 | 18 | do { 19 | intCurrent = intSquares(intCurrent); 20 | intNext = intSquares(intSquares(intNext)); 21 | } while (intCurrent != intNext); 22 | 23 | return intCurrent == 1; 24 | } 25 | 26 | public static void main(String[] args) { 27 | Scanner sc = new Scanner(System.in); 28 | 29 | System.out.print("Enter a number: "); 30 | int intNumber = sc.nextInt(); 31 | 32 | if (!boolHappyNumber(intNumber)) { 33 | System.out.println("["+intNumber+"] is NOT a Happy Number :("); 34 | } else { 35 | System.out.println("["+intNumber+"] is a Happy Number :)"); 36 | } 37 | 38 | sc.close(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Lecture 3 OOP Concepts/Encapsulation/BasedOnDataMethod.java: -------------------------------------------------------------------------------- 1 | // Example of encapsulation by bundling data and methods 2 | class Student { 3 | // Data encapsulation (private fields) 4 | private String name; 5 | private int age; 6 | 7 | // Method encapsulation (public methods to access data) 8 | public void setName(String name) { 9 | this.name = name; // controlled access 10 | } 11 | 12 | public String getName() { 13 | return name; 14 | } 15 | 16 | public void setAge(int age) { 17 | if (age > 0) { // validation inside method 18 | this.age = age; 19 | } else { 20 | System.out.println("Invalid age!"); 21 | } 22 | } 23 | 24 | public int getAge() { 25 | return age; 26 | } 27 | } 28 | 29 | public class BasedOnDataMethod { 30 | public static void main(String[] args) { 31 | Student s = new Student(); 32 | 33 | // Using methods to interact with hidden data 34 | s.setName("Chris"); 35 | s.setAge(25); 36 | 37 | System.out.println("Name: " + s.getName()); 38 | System.out.println("Age: " + s.getAge()); 39 | 40 | s.setAge(-5); // Invalid value, blocked by method 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /MP2/MP-G9/Item10.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class Item10 { 5 | public static boolean areAnagrams(String strFirst, String strSecond) { 6 | strFirst = strFirst.replaceAll("\\s", "").toLowerCase(); 7 | strSecond = strSecond.replaceAll("\\s", "").toLowerCase(); 8 | 9 | char[] arrFirstChar = strFirst.toCharArray(); 10 | char[] arrSecondChar = strSecond.toCharArray(); 11 | 12 | Arrays.sort(arrFirstChar); 13 | Arrays.sort(arrSecondChar); 14 | 15 | return Arrays.equals(arrFirstChar, arrSecondChar); 16 | } 17 | 18 | public static void main(String[] args) { 19 | Scanner scInput = new Scanner(System.in); 20 | 21 | System.out.print("Enter the first string: "); 22 | String strFirstInput = scInput.nextLine(); 23 | 24 | System.out.print("Enter the second string: "); 25 | String strSecondInput = scInput.nextLine(); 26 | 27 | if (areAnagrams(strFirstInput, strSecondInput)) { 28 | System.out.println("The strings are anagrams."); 29 | } else { 30 | System.out.println("The strings are not anagrams."); 31 | } 32 | 33 | scInput.close(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /MP2/MP2-G10/Item3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item3 { 4 | public static int factorial (int intnum) { 5 | int intFactorial = 1; 6 | 7 | for (int i = 1; i <= intnum; i++) { 8 | intFactorial *= i; 9 | } 10 | return intFactorial; 11 | } 12 | 13 | public static int digit (int intn) { 14 | int intTotal = 0; 15 | while (intn > 0) { 16 | int intDigit = intn % 10; 17 | int intDigitFactorial = factorial (intDigit); 18 | intTotal += intDigitFactorial; 19 | intn /= 10; 20 | } 21 | 22 | return intTotal; 23 | } 24 | 25 | public static void main (String[] args) { 26 | Scanner sc = new Scanner(System.in); 27 | 28 | int intNumber = 0; 29 | while (true) { 30 | System.out.print("Hello! Please enter a number: "); 31 | 32 | if (sc.hasNextInt()) { 33 | intNumber = sc.nextInt(); 34 | 35 | if (intNumber > 0) { 36 | break; 37 | } 38 | } 39 | sc.next(); 40 | } 41 | 42 | if (digit (intNumber) == intNumber) { 43 | System.out.println(intNumber + " is a Strong Number"); 44 | } 45 | 46 | else { 47 | System.out.println(intNumber + " is not strong number" ); 48 | } 49 | 50 | sc.close(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item8.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item8{ 4 | public static void main(String[] args){ 5 | Scanner sc = new Scanner(System.in); 6 | 7 | int intn = 0; 8 | boolean valid = false; 9 | 10 | while (!valid) { 11 | System.out.print("Enter an integer: "); 12 | if (sc.hasNextInt()) { 13 | intn = sc.nextInt(); 14 | valid = true; 15 | } else { 16 | System.out.println("Invalid input! Please enter an integer only."); 17 | sc.next(); 18 | } 19 | }//loops the user until a integer value is entered 20 | 21 | int square = intn * intn; 22 | String strN = String.valueOf(intn);//turns the input to a string 23 | String squareStr = String.valueOf(square);//turns the square of the input to a string 24 | 25 | if(squareStr.endsWith(strN) == true){//checks if the input is at the of its square AKA automorphic 26 | System.out.printf("%d is an AUTOMORPHIC number = %d = %d", intn, square, intn); 27 | } 28 | else{ 29 | System.out.print("Your number is NOT an AUTOMORPHIC number"); 30 | } 31 | 32 | } 33 | } -------------------------------------------------------------------------------- /MP2/group007-MP2/Item6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item6 { 4 | 5 | public static void main(String[] args) { 6 | Scanner scan = new Scanner(System.in); 7 | 8 | System.out.print("Enter a number to get its prime factors: "); 9 | int intNumber = scan.nextInt(); 10 | int intCopyOfNumber = intNumber, intSmallestPrime = 2; 11 | 12 | LessThanTwo(intNumber); 13 | 14 | System.out.print("The prime factors of " + intNumber + " are: "); 15 | 16 | GetPrimeFactors(intCopyOfNumber, intSmallestPrime); 17 | 18 | scan.close(); 19 | 20 | } 21 | 22 | static void LessThanTwo(int intNumber) { 23 | if (intNumber < 2) 24 | { 25 | System.out.println("This number does not have any prime factors."); 26 | } 27 | } 28 | 29 | static void GetPrimeFactors(int intCopyOfNumber, int intSmallestPrime) { 30 | while (intCopyOfNumber > 1) 31 | { 32 | while (intCopyOfNumber % intSmallestPrime == 0) 33 | { 34 | System.out.print(intSmallestPrime + " "); 35 | intCopyOfNumber /= intSmallestPrime; 36 | } 37 | intSmallestPrime++; 38 | } 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /MP2/group007-MP2/Item3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item3 { 4 | 5 | public static void main(String[] args) { 6 | 7 | // Asks for user input 8 | Scanner objScanner = new Scanner(System.in); 9 | System.out.print("Enter a number: "); 10 | int number = objScanner.nextInt(); 11 | 12 | int originalNumber = number; 13 | 14 | int sumOfFactorial = 0; 15 | int digitOfNum; 16 | 17 | while (number > 0) { 18 | 19 | digitOfNum = number % 10; 20 | if (digitOfNum == 0) { // Handles zero factorial 21 | digitOfNum = 1; 22 | } 23 | 24 | for (int i = digitOfNum - 1; i > 0; i--) { 25 | digitOfNum = digitOfNum * i; 26 | } 27 | 28 | sumOfFactorial = sumOfFactorial + digitOfNum; 29 | 30 | number = number / 10; 31 | } 32 | 33 | if (originalNumber == sumOfFactorial) { 34 | System.out.println(originalNumber + " is a strong number!"); 35 | } 36 | else { 37 | System.out.println(originalNumber + " is not strong number"); 38 | } 39 | 40 | 41 | objScanner.close(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item2{ 4 | public static void main(String[] args){ 5 | Scanner sc = new Scanner(System.in); 6 | 7 | int intn = 0; 8 | boolean valid = false; 9 | 10 | while (!valid) { 11 | System.out.print("Enter an integer: "); 12 | if (sc.hasNextInt()) { 13 | intn = sc.nextInt(); 14 | valid = true; 15 | } else { 16 | System.out.println("Invalid input! Please enter an integer only."); 17 | sc.next(); 18 | } 19 | } //loops the input section until the user puts an integer 20 | 21 | int placeHolder = intn; 22 | int sum = 0; 23 | 24 | while(placeHolder > 0){ 25 | int lastDigit = placeHolder % 10; 26 | sum += lastDigit; 27 | placeHolder = placeHolder / 10; 28 | } //adds every digit from the right side to the sum and remove it until there are no numbers left 29 | 30 | 31 | if(intn % sum == 0){ 32 | System.out.println("Your number is a HARSHAD number."); 33 | } 34 | else{ 35 | System.out.println("Your number is NOT a HARSHAD number."); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /MP2/MP-G1/Item12.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /*"Word Count in a Sentence" 4 | Ask the user to input a sentence and count how many words it contains.*/ 5 | 6 | public class Item12 { 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | String strSentence; 10 | System.out.println("WORD COUNT SIMULATOR"); 11 | 12 | while (true) { 13 | System.out.print("Enter a sentence: "); 14 | strSentence = sc.nextLine().trim(); 15 | // Check if input is empty 16 | if (strSentence.isEmpty()) { 17 | System.out.println("Please enter a sentence.\n"); 18 | continue; 19 | } 20 | // Check if input is purely numeric (integer or decimal) 21 | if (strSentence.matches("\\d+(\\.\\d+)?")) { 22 | System.out.println("Invalid input. Please enter a valid sentence.\n"); 23 | continue; 24 | } 25 | break; // Valid input 26 | } 27 | // Count words 28 | String[] strWords = strSentence.split("\\s+"); 29 | System.out.println(" Word count: " + strWords.length); 30 | sc.close(); 31 | System.out.println("Program Ended Succesfully."); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /MP2/MP-G4/Item14.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item14 { 4 | public static void main (String[] args){ 5 | Scanner sc = new Scanner(System.in); 6 | 7 | System.out.print("Enter number of rows: "); 8 | int intRow = sc.nextInt(); 9 | System.out.print("Enter number of columns: "); 10 | int intCol = sc.nextInt(); 11 | 12 | int[][] matrix = new int[intRow][intCol]; 13 | 14 | System.out.println("\nEnter elements of the matrix:"); 15 | for (int i = 0; i < intRow; i++) { 16 | for (int j = 0; j < intCol; j++) { 17 | System.out.print("Row " + (i + 1) + ", Column " + (j + 1) + ": "); 18 | matrix[i][j] = sc.nextInt(); 19 | } 20 | } 21 | 22 | int[][] transposedMatrix = new int[intCol][intRow]; 23 | 24 | for (int i = 0; i < intRow; i++) { 25 | for (int j = 0; j < intCol; j++) { 26 | transposedMatrix[j][i] = matrix[i][j]; 27 | } 28 | } 29 | 30 | System.out.println("\nTransposed Matrix:"); 31 | for (int i = 0; i < intCol; i++) { 32 | for (int j = 0; j < intRow; j++) { 33 | System.out.print(transposedMatrix[i][j] + " "); 34 | } 35 | System.out.println(); 36 | } 37 | 38 | 39 | sc.close(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /MP2/group007-MP2/Item12.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item12 { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | System.out.print("Enter a sentence: "); 7 | String strSentence = scanner.nextLine(); 8 | 9 | if (strSentence.isEmpty()) { // Error handling for empty user input 10 | System.out.println("The sentence is empty. Please enter a valid sentence."); 11 | return; 12 | } 13 | 14 | int intSentenceLength = strSentence.length(); // String manipulation to get sentence length 15 | int intWordCount = 0; 16 | boolean isWord = false; // Determines if the program is in a word 17 | 18 | for (int i = 0; i < intSentenceLength; i++) { 19 | char chCurrentChar = strSentence.charAt(i); 20 | if (Character.isLetter(chCurrentChar)) { // Character manipulation to check if character is a letter 21 | if (!isWord) { 22 | intWordCount++; 23 | isWord = true; 24 | } 25 | } else { 26 | isWord = false; 27 | } 28 | } 29 | System.out.println("Number of words in the sentence: " + intWordCount); 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /MP2/MP2-G3/Item11.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item11 { 4 | public static void main(String[] args) { 5 | Scanner scan = new Scanner(System.in); 6 | System.out.print("Enter a string: "); 7 | 8 | // Ensure non-empty input 9 | String input = scan.nextLine().trim(); 10 | while (input.isEmpty()) { 11 | System.out.println("Invalid input! Please enter a non-empty string."); 12 | System.out.print("Enter a string: "); 13 | input = scan.nextLine().trim(); 14 | } 15 | 16 | input = input.toLowerCase(); 17 | 18 | int vowelCount = 0; 19 | int consonantCount = 0; 20 | 21 | for (int i = 0; i < input.length(); i++) { 22 | char letter = input.charAt(i); 23 | if (letter >= 'a' && letter <= 'z') { 24 | if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u') { 25 | vowelCount++; 26 | } else { 27 | consonantCount++; 28 | } 29 | } 30 | } 31 | 32 | System.out.println("Number of vowels: " + vowelCount); 33 | System.out.println("Number of consonants: " + consonantCount); 34 | scan.close(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /MP2/MP2-G3/Item5.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item5 { 4 | public static void main(String[] args) { 5 | System.out.print("Input Number: "); 6 | Scanner scan = new Scanner(System.in); 7 | 8 | //Ensure only valid long input 9 | while (!scan.hasNextLong()) { 10 | System.out.println("Invalid input! Please enter a whole number."); 11 | System.out.print("Input Number: "); 12 | scan.next(); // clear invalid input 13 | } 14 | 15 | long num = scan.nextLong(); 16 | long squared = num * num; 17 | String str = Long.toString(squared); 18 | int length = str.length(); 19 | boolean isKaprekar = false; 20 | 21 | for (int i = 1; i < length; i++) { 22 | long leftSide = Long.parseLong(str.substring(0, i)); 23 | long rightSide = Long.parseLong(str.substring(i)); 24 | if (rightSide > 0 && leftSide + rightSide == num) { 25 | isKaprekar = true; 26 | break; 27 | } 28 | } 29 | if (isKaprekar) 30 | System.out.printf("%d is a Kaprekar Number", num); 31 | else 32 | System.out.printf("%d is not a Kaprekar Number", num); 33 | 34 | scan.close(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /MP2/MP-G1/Item1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | //Perfect Number checker 3 | public class Item1{ 4 | public static void main(String[] args){ 5 | Scanner scanner = new Scanner(System.in); //Scanner object 6 | int intNum = 0; 7 | 8 | while (true) { 9 | System.out.print("Enter a number: "); 10 | if (!scanner.hasNextInt()) { 11 | System.out.print("\tInvalid.\n\tPlease enter a valid integer.\n"); 12 | scanner.next(); 13 | continue; 14 | } 15 | intNum = scanner.nextInt(); 16 | 17 | if (intNum <= 0) { 18 | System.out.print("\tInvalid.\n\tPlease enter a positive integer only.\n"); 19 | continue; 20 | } 21 | break; 22 | } 23 | 24 | int sumDivisors = 0; 25 | for (int i = 1; i <= intNum / 2; i++) { 26 | if (intNum % i == 0) 27 | sumDivisors += i; 28 | } 29 | if(sumDivisors == intNum){ 30 | System.out.println(intNum + " is a perfect number."); 31 | } else { 32 | System.out.println(intNum + " is not a perfect number."); 33 | } 34 | 35 | scanner.close(); //close the scanner 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MP2/group007-MP2/Item5.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item5 { 4 | 5 | public static void main (String[] args) { 6 | 7 | Scanner objScanner = new Scanner(System.in); 8 | System.out.print("Enter number: "); 9 | int number = objScanner.nextInt(); 10 | 11 | int numberSquared = (int) Math.pow(number, 2); 12 | int originalNumber = numberSquared; 13 | 14 | // To count how many digits the squared number has 15 | int i = 0; 16 | while (numberSquared > 0) { 17 | numberSquared = numberSquared / 10; 18 | i++; 19 | } 20 | 21 | // Turned into a float first for cases where the number is not divided evenly i.e. 297^2 = 88209 22 | float halfDigits = (float) i/2; 23 | int right = (int) Math.round(halfDigits); 24 | 25 | int rightValue = originalNumber % (int) Math.pow(10, right); 26 | int leftValue = originalNumber / (int) Math.pow(10, right); 27 | 28 | if ((leftValue + rightValue) == number) { 29 | System.out.println(number + " is a Kaprekar number!"); 30 | } 31 | else { 32 | System.out.println(number + " is not a Kaprekar number"); 33 | } 34 | 35 | objScanner.close(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Lecture 3 OOP Concepts/Encapsulation/EncapsulationGetterSetter.java: -------------------------------------------------------------------------------- 1 | class Student { 2 | // Private fields (hidden from outside classes) 3 | private String name; 4 | private int age; 5 | 6 | // Getter for name 7 | public String getName() { 8 | return name; 9 | } 10 | 11 | // Setter for name 12 | public void setName(String name) { 13 | this.name = name; 14 | } 15 | 16 | // Getter for age 17 | public int getAge() { 18 | return age; 19 | } 20 | 21 | // Setter for age with validation 22 | public void setAge(int age) { 23 | if (age > 0) { 24 | this.age = age; 25 | } else { 26 | System.out.println("Age must be positive!"); 27 | } 28 | } 29 | } 30 | 31 | // Main class 32 | public class EncapsulationGetterSetterExample { 33 | public static void main(String[] args) { 34 | Student student = new Student(); 35 | 36 | // Set values using setters 37 | student.setName("Chris"); 38 | student.setAge(22); 39 | 40 | // Access values using getters 41 | System.out.println("Student Name: " + student.getName()); 42 | System.out.println("Student Age: " + student.getAge()); 43 | 44 | // Test invalid value 45 | student.setAge(-5); // Validation prevents setting invalid age 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /MP2/MP2-G3/Item6.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 6. Prime Factors of a Number 3 | * Ask the user to enter a number and display 4 | * all its prime factors. For example, the prime 5 | * factors of 84 are 2, 2, 3, 7. 6 | */ 7 | 8 | import java.util.*; 9 | 10 | public class Item6 { 11 | public static void main(String[] args) { 12 | // Using Scanner 13 | Scanner objScanner = new Scanner(System.in); 14 | 15 | System.out.println("Program to Display a Number's Prime Factors"); 16 | 17 | // get input from user 18 | System.out.print("Enter a number: "); 19 | 20 | // Ensure only integer input 21 | while (!objScanner.hasNextInt()) { 22 | System.out.println("Invalid input! Please enter an integer."); 23 | System.out.print("Enter a number: "); 24 | objScanner.next(); // clear invalid input 25 | } 26 | 27 | int number = objScanner.nextInt(); 28 | 29 | int intI = 2; 30 | 31 | while (intI * intI <= number) { 32 | while (number % intI == 0) { 33 | System.out.print(intI + " "); 34 | number = number / intI; 35 | } 36 | intI = intI + 1; 37 | } 38 | 39 | if (number > 1) { 40 | System.out.print(number + " "); 41 | } 42 | 43 | objScanner.close(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /MP2/MP2-G6/Item10.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class Item10 { 5 | 6 | public static boolean isAnagram(String strFirstString, String strSecondString) { 7 | 8 | strFirstString = strFirstString.toLowerCase(); 9 | strSecondString = strSecondString.toLowerCase(); 10 | 11 | if (strFirstString.length() != strSecondString.length()) { 12 | return false; 13 | } 14 | 15 | char[] charFirstArray = strFirstString.toCharArray(); 16 | char[] charSecondArray = strSecondString.toCharArray(); 17 | 18 | Arrays.sort(charFirstArray); 19 | Arrays.sort(charSecondArray); 20 | 21 | 22 | return Arrays.equals(charSecondArray, charFirstArray); 23 | } 24 | 25 | public static void main(String[] args) { 26 | Scanner scanner = new Scanner(System.in); 27 | 28 | System.out.print("Enter the first string: "); 29 | String strFirstString = scanner.nextLine(); 30 | 31 | System.out.print("Enter the second string: "); 32 | String strSecondString = scanner.nextLine(); 33 | 34 | if (isAnagram(strFirstString, strSecondString)) { 35 | System.out.println("is ANAGRAM :>"); 36 | } else { 37 | System.out.println("Is NOT Anagram :2"); 38 | } 39 | 40 | scanner.close(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /MP2/MP2-G6/Item4.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | 5 | 6 | public class Item4{ 7 | 8 | static int squareSum(int number){ 9 | int sum = 0; 10 | while (number > 0){ 11 | int digit = number % 10; 12 | sum += digit*digit; 13 | number/=10; 14 | } 15 | return sum; 16 | } 17 | static boolean isHappyNumber(int number){ 18 | int tentativeSum = number; 19 | boolean boolIsHappyNumber = false; 20 | while (tentativeSum > 1){ 21 | tentativeSum = squareSum(tentativeSum); 22 | if(tentativeSum == number) return boolIsHappyNumber; 23 | } 24 | boolIsHappyNumber = true; 25 | 26 | return boolIsHappyNumber; 27 | } 28 | public static void main(String[] args) { 29 | Scanner scanner = new Scanner(System.in); 30 | System.out.print("||========||Happy Number Checker||========||\nEnter a number: "); 31 | if(scanner.hasNextInt()){ 32 | int number = scanner.nextInt(); 33 | System.out.println((isHappyNumber(number)) ? ("Number " + number + " is a Happy Number :>") : ("Number " + number + " is NOT a Happy Number :<")); 34 | } else { 35 | System.out.println("Input is not a valid number"); 36 | } 37 | System.out.print("||========================================||"); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /MP2/MP-G9/Item5.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | // Kaprekar Number 3 | 4 | public class Item5 { 5 | 6 | static boolean boolKaprekar (int intNumber) { 7 | if (intNumber == 1) { 8 | return true; 9 | } 10 | 11 | long lngSquare = (long) intNumber * intNumber; // long to avoid overflow 12 | String strSquare = Long.toString(lngSquare); 13 | int intLength = strSquare.length(); 14 | 15 | // split 16 | for (int i = 1; i < intLength; i++) { 17 | String strLeft = strSquare.substring(0, i); 18 | String strRight = strSquare.substring(i); 19 | 20 | int intLeft = Integer.parseInt(strLeft); 21 | int intRight = Integer.parseInt(strRight); 22 | 23 | if (intLeft + intRight == intNumber && intRight != 0) { 24 | return true; 25 | } 26 | } 27 | return false; 28 | } 29 | 30 | public static void main(String[] args) { 31 | Scanner sc = new Scanner(System.in); 32 | 33 | System.out.print("Enter a number: "); 34 | int intNumber = sc.nextInt(); 35 | 36 | if (boolKaprekar(intNumber)) { 37 | System.out.println("["+intNumber+"] IS a Kaprekar Number."); 38 | } else { 39 | System.out.println("["+intNumber+"] is NOT a Kaprekar Number."); 40 | } 41 | 42 | sc.close(); 43 | } 44 | } -------------------------------------------------------------------------------- /MP2/MP-G4/Item11.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item11 { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | 7 | System.out.print("Enter a string: "); 8 | String str = scanner.nextLine().trim(); 9 | 10 | System.out.println("Word: " + str); 11 | System.out.println("Vowel Count: " + countVowel(str.toLowerCase())); 12 | System.out.println("Consonant Count: " + countConsonant(str.toLowerCase())); 13 | 14 | scanner.close(); 15 | } 16 | 17 | public static int countVowel(String str) { 18 | int vowelCount = 0; 19 | 20 | for (int i = 0; i < str.length(); i++) { 21 | char ch = str.charAt(i); 22 | if (isVowel(ch)) { 23 | vowelCount++; 24 | } 25 | } 26 | 27 | return vowelCount; 28 | } 29 | 30 | public static int countConsonant(String str) { 31 | int consonantCount = 0; 32 | 33 | for (int i = 0; i < str.length(); i++) { 34 | char ch = str.charAt(i); 35 | if ((ch >= 'a' && ch <= 'z') && !isVowel(ch)) { 36 | consonantCount++; 37 | } 38 | } 39 | 40 | return consonantCount; 41 | } 42 | 43 | public static boolean isVowel(char ch) { 44 | return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /MP2/MP2-G10/Item7.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item7 { 4 | public static void main (String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | int intLowerLimit = 0; 8 | while (true) { 9 | System.out.print("Please enter the lower limit: "); 10 | if (sc.hasNextInt()) { 11 | intLowerLimit = sc.nextInt(); 12 | 13 | if (intLowerLimit > 0) { 14 | break; 15 | } 16 | } 17 | sc.next(); 18 | } 19 | 20 | int intUpperLimit = 0; 21 | while (true) { 22 | System.out.print(" Please enter the upper limit: "); 23 | if (sc.hasNextInt()) { 24 | intUpperLimit = sc.nextInt(); 25 | 26 | if (intUpperLimit > 0) { 27 | break; 28 | } 29 | } 30 | sc.next(); 31 | } 32 | sc.close(); 33 | 34 | System.out.println("Here are the twin prime numbers between " + intLowerLimit + " and " + intUpperLimit); 35 | 36 | for (int i = intLowerLimit; i <= intUpperLimit -2; i++ ) { 37 | if (prime(i) && prime(i + 2)) { 38 | System.out.println(i + " and " + (i+2)); 39 | } 40 | } 41 | } 42 | 43 | public static boolean prime (int intN) { 44 | if (intN <= 1) { 45 | return false; 46 | } 47 | for (int i = 2; i <= Math.sqrt(intN); i++) { 48 | if (intN % i == 0) { 49 | return false; 50 | } 51 | } 52 | return true; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /MP2/MP2-G5/Item5.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class KaprekarNumber { 4 | public static void main(String[] args) { 5 | Scanner scan = new Scanner(System.in); 6 | 7 | // Input number 8 | System.out.print("Enter a number: "); 9 | int number = scan.nextInt(); 10 | 11 | // Check Kaprekar 12 | if (isKaprekar(number)) { 13 | System.out.println(number + " is a Kaprekar Number."); 14 | } else { 15 | System.out.println(number + " is NOT a Kaprekar Number."); 16 | } 17 | 18 | scan.close(); 19 | } 20 | 21 | // Function to check Kaprekar 22 | public static boolean isKaprekar(int n) { 23 | if (n == 1) return true; // 1 is kaprekar by definition 24 | 25 | long square = (long) n * n; 26 | String str = Long.toString(square); 27 | 28 | for (int i = 1; i < str.length(); i++) { 29 | // splitting the square into two parts 30 | String left = str.substring(0, i); 31 | String right = str.substring(i); 32 | 33 | long leftNumber = (left.isEmpty()) ? 0 : Long.parseLong(left); 34 | long rightNumber = (right.isEmpty()) ? 0 : Long.parseLong(right); 35 | 36 | if (leftNumber + rightNumber == n && rightNumber != 0) { 37 | return true; 38 | } 39 | } 40 | return false; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item13.java: -------------------------------------------------------------------------------- 1 | public class MatrixMultiplication { 2 | public static void main(String[] args){ 3 | int[][] matrixOne = { //replacable with other integers 4 | {1, 2, 3}, 5 | {4, 5, 6} 6 | }; 7 | 8 | int[][] matrixTwo = { //replacable with other integers 9 | {7, 10}, 10 | {8, 11}, 11 | {9, 12} 12 | }; 13 | 14 | int[][] result = multiplyMatrix(matrixOne, matrixTwo); 15 | 16 | System.out.println("Product of the two matrices:"); 17 | for (int[] row : result) { 18 | for int value : row) { 19 | System.out.println(value + ", "); 20 | } 21 | System.out.println(); 22 | } 23 | } 24 | 25 | public static int[][] multiplyMatrix(int[][] matrixOne, int[][] matrixTwo) { 26 | int rowOne = matrixOne.length; 27 | int columnOne = matrixOne[0].length; 28 | int rowTwo = matrixTwo.length; 29 | int columnTwo = matrixTwo[0].length; 30 | 31 | int[][] product = new int[rowOne][columnTwo]; 32 | 33 | for (int i = 0; i < rowOne; i++) { 34 | for (int j = 0; j < columnTwo; j++) { 35 | for (int k = 0; k < columnOne; k++) { 36 | product[i][j] += matrixOne[i][k] * matrixTwo[k][j]; 37 | } 38 | } 39 | } 40 | 41 | return product; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /MP2/MP-G4/Item10.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item10 { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | 7 | System.out.print("Enter first string: "); 8 | String str1 = scanner.nextLine().trim(); 9 | System.out.print("Enter second string: "); 10 | String str2 = scanner.nextLine().trim(); 11 | 12 | if (areAnagram(str1.toLowerCase(), str2.toLowerCase())) { 13 | System.out.println(str1 + " and " + str2 + " are Anagram"); 14 | } else { 15 | System.out.println(str1 + " and " + str2 + " are not Anagram"); 16 | } 17 | 18 | scanner.close(); 19 | } 20 | 21 | public static boolean areAnagram(String str1, String str2) { 22 | int[] arr = new int[26]; 23 | 24 | for (int i = 0; i < str1.length(); i++) { 25 | char ch = str1.charAt(i); 26 | if (ch >= 'a' && ch <= 'z') { 27 | arr[ch - 'a']++; 28 | } 29 | } 30 | 31 | for (int i = 0; i < str2.length(); i++) { 32 | char ch = str2.charAt(i); 33 | if (ch >= 'a' && ch <= 'z') { 34 | arr[ch - 'a']--; 35 | } 36 | } 37 | 38 | for (int i = 0; i < 26; i++) { 39 | if (arr[i] != 0) { 40 | return false; 41 | } 42 | } 43 | 44 | return true; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /MP2/MP-G4/Item5.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item5 { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | int number; 7 | 8 | // Ask user for a positive number input 9 | while (true) { 10 | System.out.print("Enter a number: "); 11 | number = scanner.nextInt(); 12 | 13 | if (number > 0) { 14 | break; 15 | } 16 | 17 | System.out.println("Invalid Input. Must be positive number."); 18 | } 19 | 20 | if (isKaprekarNumber(number)) { 21 | System.out.println(number + " is a Kaprekar number"); 22 | } else { 23 | System.out.println(number + " is not a Kaprekar number"); 24 | } 25 | 26 | scanner.close(); 27 | } 28 | 29 | public static boolean isKaprekarNumber(int number) { 30 | if (number == 1) { 31 | return true; 32 | } 33 | 34 | int squaredNumber = number * number; 35 | int numberLength = String.valueOf(number).length(); 36 | 37 | if (String.valueOf(squaredNumber).length() == 1) { 38 | return false; 39 | } 40 | 41 | int divider = (int) Math.pow(10, numberLength); 42 | int rightPart = squaredNumber % divider; 43 | int leftPart = squaredNumber / divider; 44 | return rightPart + leftPart == number; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /MP2/MP2-G6/Item1.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | 4 | public class Item1 { 5 | 6 | public static int sumOfDivisors(int intNumber, int intSum) { 7 | 8 | for (int intIndex = 1; intIndex < intNumber; intIndex++) { 9 | if (intNumber % intIndex == 0) { 10 | intSum += intIndex; 11 | } 12 | } 13 | 14 | if (intSum == intNumber) { 15 | System.out.println(intNumber + " is a perfect number."); 16 | } else { 17 | System.out.println(intNumber + " is not a perfect number."); 18 | } 19 | 20 | return 0; 21 | } 22 | 23 | public static void main(String args[]) { 24 | int intNumber = 0; 25 | int intSum = 0; 26 | 27 | Scanner console = new Scanner(System.in); 28 | System.out.print("Enter a positive integer: "); 29 | while (true) { 30 | if (console.hasNextInt()) { 31 | intNumber = console.nextInt(); 32 | if (intNumber <= 0) { 33 | System.out.print("Please enter a positive integer: "); 34 | } else { 35 | break; 36 | } 37 | } else { 38 | System.out.print("Invalid input. Please enter a positive integer: "); 39 | console.next(); 40 | } 41 | } 42 | 43 | sumOfDivisors(intNumber, intSum); 44 | 45 | console.close(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /MP2/group007-MP2/Item11.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item11 { 4 | static boolean isAVowel(char n) { // Method to check if character is a vowel 5 | n = Character.toLowerCase(n); 6 | return (n == 'a' || n == 'e' || n == 'i' || n == 'o' || n == 'u'); 7 | } 8 | public static void main(String[] args) { 9 | Scanner scanner = new Scanner(System.in); 10 | System.out.print("Enter a string: "); 11 | String strUserString = scanner.nextLine(); 12 | 13 | if (strUserString.trim().isEmpty()) { // Checks if string is empty or only spaces 14 | System.out.println("The string is empty."); 15 | return; 16 | } 17 | int intVowelCount = 0; 18 | int intConsonantCount = 0; 19 | strUserString = strUserString.toLowerCase(); // Conversion to lowercase for easier comparison 20 | 21 | for (int i = 0; i < strUserString.length(); i++) { // Loops through each character 22 | char currentCharacter = strUserString.charAt(i); // Gets the current character 23 | if (Character.isLetter(currentCharacter)) { // Character manipulation to check if it's a letter 24 | if (isAVowel(currentCharacter)) { 25 | intVowelCount++; 26 | } else { 27 | intConsonantCount++; 28 | } 29 | } 30 | } 31 | System.out.println("Number of vowels: " + intVowelCount); 32 | System.out.println("Number of consonants: " + intConsonantCount); 33 | } 34 | } -------------------------------------------------------------------------------- /MP2/MP-G4/Item15.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class Item15 { 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | 9 | System.out.print("Enter the number of elements: "); 10 | int intSize = sc.nextInt(); 11 | 12 | int[] arr = new int[intSize]; 13 | 14 | System.out.println("Enter the elements:"); 15 | for (int i = 0; i < intSize; i++) { 16 | System.out.print("Element " + (i + 1) + ": "); 17 | arr[i] = sc.nextInt(); 18 | } 19 | System.out.println(); 20 | 21 | Arrays.sort(arr); 22 | 23 | System.out.print("Elements (sorted): "); 24 | for (int i = 0; i < intSize; i++){ 25 | System.out.print(arr[i]); 26 | if (i != intSize - 1) { // only print comma if not last element 27 | System.out.print(", "); 28 | } 29 | } 30 | System.out.println("\n"); 31 | 32 | int count = 1; 33 | for (int i = 1; i < intSize; i++) { 34 | if (arr[i] == arr[i - 1]) { 35 | count++; 36 | } else { 37 | System.out.println("Element " + arr[i - 1] + " --- " + count + " time(s)."); 38 | count = 1; 39 | } 40 | } 41 | 42 | System.out.println("Element " + arr[intSize - 1] + " --- " + count + " time(s)."); 43 | 44 | sc.close(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item11.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item11 { 4 | public static void main(String[] args){ 5 | Scanner scanner=new Scanner(System.in); 6 | int intVowelCount=0; 7 | int intConsonantCount=0; 8 | 9 | System.out.print("Enter a string: "); 10 | String strInput = scanner.nextLine(); 11 | 12 | strInput = strInput.toLowerCase(); //for easier checking 13 | boolean boolLetterFound=false; //for checking if letters are present 14 | 15 | for(int i=0; i='a'&&charCharCheck<='z'){ 18 | if(charCharCheck=='a'||charCharCheck=='e'||charCharCheck=='i'||charCharCheck=='o'||charCharCheck=='u'){ 19 | intVowelCount++; 20 | boolLetterFound=true; 21 | }else{ 22 | intConsonantCount++; 23 | boolLetterFound=true; 24 | } 25 | 26 | } 27 | 28 | } 29 | 30 | System.out.println("Vowels found in the string: "+intVowelCount); 31 | System.out.println("Consonants found in the string: "+intConsonantCount); 32 | if(!boolLetterFound){ //if no letters were found in the string 33 | System.out.println("There are no vowels nor consonants in the string."); 34 | } 35 | scanner.close(); 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /MP2/MP-G9/Item15.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item15 { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | 7 | System.out.print("Enter how many numbers: "); 8 | int n = scanner.nextInt(); 9 | int[] intNumbers = new int[n]; 10 | int[] intFrequency = new int[n]; 11 | int intCounted = -1; 12 | 13 | System.out.println("Enter " + n + " numbers:"); 14 | for (int i = 0; i < n; i++) { 15 | System.out.print("Number " + (i + 1) + ": "); 16 | intNumbers[i] = scanner.nextInt(); 17 | } 18 | 19 | for (int i = 0; i < n; i++) { 20 | if (intFrequency[i] == intCounted) 21 | continue; 22 | 23 | int count = 1; 24 | for (int j = i + 1; j < n; j++) { 25 | if (intNumbers[i] == intNumbers[j]) { 26 | count++; 27 | intFrequency[j] = intCounted; 28 | } 29 | } 30 | intFrequency[i] = count; 31 | } 32 | 33 | System.out.println("\nElements with the Frequencies:"); 34 | System.out.println("-------------------"); 35 | for (int i = 0; i < n; i++) { 36 | if (intFrequency[i] != intCounted) { 37 | System.out.printf("Element [%d] - has a frequency of %d\n-------------------\n", intNumbers[i], intFrequency[i] ); 38 | } 39 | } 40 | 41 | scanner.close(); 42 | } 43 | } -------------------------------------------------------------------------------- /MP2/MP2_Grp02/Item14/transposeMatrix.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class transposeMatrix { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | System.out.print("Enter the number of rows of the matrix : "); 8 | int x = sc.nextInt(); 9 | System.out.print("Enter the number of columns of the matrix : "); 10 | int y = sc.nextInt(); 11 | 12 | int[][] arr = new int[x][y]; 13 | int[][] transposeArr = new int[x][y]; 14 | 15 | System.out.println("Enter elements of the array: "); 16 | for (int i = 0; i < x; i++) { 17 | for (int j = 0; j < y; j++) { 18 | System.out.print("Enter element [" + i + "][" + j + "] of the array : "); 19 | arr[i][j] = sc.nextInt(); 20 | } 21 | } 22 | 23 | for (int i = 0; i < x; i++) { 24 | for (int j = 0; j < y; j++) { 25 | transposeArr[i][j] = arr[j][i]; 26 | } 27 | } 28 | 29 | System.out.println("\nOriginal Matrix: "); 30 | for (int i = 0; i < x; i++) { 31 | for (int j = 0; j < y; j++) { 32 | System.out.print(arr[i][j] + " "); 33 | } 34 | } 35 | 36 | System.out.println("\nTranspose of the Matrix: "); 37 | for (int i = 0; i < x; i++) { 38 | for (int j = 0; j < y; j++) { 39 | System.out.print(transposeArr[i][j] + " "); 40 | } 41 | } 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /MP2/MP2-G8/Item1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item1 { 4 | public static void main(String[] args){ 5 | int intNumber; 6 | Scanner scanner = new Scanner(System.in); 7 | System.out.print("Perfect Number Check\nEnter a number: "); 8 | while(true){ //gets input and validates 9 | if(scanner.hasNextInt()){ 10 | intNumber = scanner.nextInt(); 11 | if(intNumber>0){ 12 | break; 13 | }else{ 14 | System.out.println("Enter a postive integer!"); 15 | } 16 | }else{ 17 | System.out.println("Enter a postive integer!"); 18 | scanner.next(); 19 | } 20 | } 21 | 22 | int intSumOfDivisors = 0; //stores the sum of its divisors(number itself not included) 23 | if(intNumber==1){ 24 | intSumOfDivisors = 0; 25 | }else{ 26 | intSumOfDivisors = 1; 27 | for(int i=2; i<=intNumber/2; i++){ //sums all the divisors starting from 2 up to number/2 28 | if(intNumber%i==0){ 29 | intSumOfDivisors += i; 30 | } 31 | } 32 | } 33 | 34 | 35 | 36 | if(intSumOfDivisors==intNumber){ 37 | System.out.println("This is a perfect number"); 38 | }else{ 39 | System.out.println("This is not a perfect number"); 40 | } 41 | scanner.close(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /MP2/MP-G9/Item14.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | // Matrix Transpose 3 | public class Item14 { 4 | public static void main(String[] args){ 5 | Scanner scanner = new Scanner(System.in); 6 | System.out.println("Please enter the sizes of the matrix:"); 7 | System.out.print("1st Matrix [Row]: "); 8 | int intRowOne = scanner.nextInt(); 9 | System.out.print("1st Matrix [Column]: "); 10 | int intColumnOne= scanner.nextInt(); 11 | int[][] intMatrixOne = new int[intRowOne][intColumnOne]; 12 | for(int i = 0;i < intRowOne; i++){ 13 | for(int j = 0; j< intColumnOne;j++){ 14 | System.out.printf("1st Matrix[%d][%d]: ",i,j); 15 | intMatrixOne[i][j] = scanner.nextInt(); 16 | } 17 | } 18 | // Printing the original Matrix 19 | printMatrix(intMatrixOne, intRowOne, intColumnOne, false); 20 | // Reverse the Column and Row arguements print transposed Matrix 21 | printMatrix(intMatrixOne, intColumnOne, intRowOne, true); 22 | scanner.close(); 23 | } 24 | public static void printMatrix(int[][] intMatrix, int intFirstSize, int intSecondSize, boolean boolTranspose){ 25 | System.out.println(boolTranspose ? "\nTransposed Matrix\n": "\nOriginal Matrix:\n"); 26 | 27 | for (int i = 0; i < intFirstSize; i++) { 28 | for (int j = 0; j < intSecondSize; j++) { 29 | System.out.print((boolTranspose ? intMatrix[j][i] : intMatrix[i][j]) + " "); 30 | } 31 | System.out.println(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /MP2/MP2-G6/Item2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item2 { 4 | //checks for whole number 5 | private static int getWholeNumber(Scanner numberScanner, String prompt) { 6 | //loops until whole number found 7 | while (true) { 8 | System.out.print(prompt); 9 | if (numberScanner.hasNextInt()) { 10 | return numberScanner.nextInt(); //continue with number entered 11 | } else { 12 | System.out.println("Error. Please enter a whole number."); 13 | numberScanner.next(); 14 | } 15 | } 16 | } 17 | 18 | //checks for harshad numbers 19 | static boolean isHarshad (int numberInputted) { 20 | int sum = 0; 21 | 22 | //calculates the sum of all digits by dividing the number by 10 each iteration 23 | for (int current = numberInputted; current > 0; current /= 10) { 24 | sum += current % 10; //adds the current digit 25 | } 26 | 27 | return (numberInputted % sum == 0); //return true if input is divisible by the sum 28 | } 29 | public static void main(String[] args) { 30 | Scanner numberScanner = new Scanner(System.in); 31 | 32 | int numberInputted = getWholeNumber(numberScanner, "Enter your number: "); 33 | numberScanner.close(); 34 | 35 | //print line depending on boolean return from harshad checker 36 | System.out.println(isHarshad(numberInputted) ? numberInputted + " is a Harshad number." : numberInputted + " is not a Harshad number."); 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /MP1/Group07.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ========================================================== 3 | * Machine Problem 1 - Workspace for Group 07 4 | * ========================================================== 5 | * 6 | * Instructions: 7 | * - This file is your workspace for solving Machine Problem 1. 8 | * - You will practice the Programming Core Concepts discussed in class, 9 | * such as variables, data types, operators, and basic string operations. 10 | * - Do NOT create additional classes or advanced features (like arrays) 11 | * unless instructed. 12 | * ========================================================== 13 | */ 14 | 15 | public class Group07 { 16 | public static void main(String[] args) { 17 | 18 | // Hardcoded base and exponent limit as per instructions 19 | final int BASE = 3; 20 | final int EXPONENT_LIMIT = 10; 21 | final int SUM_LIMIT = 10000; 22 | 23 | int power; 24 | int sumOfPowers = 0; 25 | 26 | for (int i = 1; i <= EXPONENT_LIMIT; i++) { 27 | power = (int) Math.pow(BASE, i); 28 | if ((sumOfPowers += power) > SUM_LIMIT) { 29 | sumOfPowers -= power; // Reverse the previous operation to get the value before it exceeded 10,000 30 | break; 31 | } 32 | else { 33 | System.out.println(BASE + "^" + i + " = " + power); 34 | } 35 | } 36 | System.out.println("Sum of powers: " + sumOfPowers); 37 | System.out.println("The succeeding power makes the sum of powers exceed 10,000"); 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /MP2/MP2-G3/Item7.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item7 { 4 | 5 | static boolean isPrime(int num){ 6 | if (num < 2) return false; 7 | for (int i=2; i * i <= num; i++){ 8 | if (num % i == 0) return false; 9 | } 10 | return true; 11 | } 12 | 13 | public static void main(String[] args){ 14 | Scanner scanner = new Scanner(System.in); 15 | 16 | System.out.print("Enter the start of the range: "); 17 | 18 | //Error handling for start input 19 | while (!scanner.hasNextInt()) { 20 | System.out.println("Invalid input! Please enter an integer."); 21 | System.out.print("Enter the start of the range: "); 22 | scanner.next(); // clear invalid input 23 | } 24 | int start = scanner.nextInt(); 25 | 26 | System.out.print("Enter the end of the range: "); 27 | 28 | //Error handling for end input 29 | while (!scanner.hasNextInt()) { 30 | System.out.println("Invalid input! Please enter an integer."); 31 | System.out.print("Enter the end of the range: "); 32 | scanner.next(); // clear invalid input 33 | } 34 | int end = scanner.nextInt(); 35 | 36 | System.out.println("The twin primes in the range are: "); 37 | for (int i = start; i <= end - 2; i++){ 38 | if(isPrime(i) && isPrime(i + 2)){ 39 | System.out.println("(" + i + ", " + (i + 2) + ")"); 40 | } 41 | } 42 | 43 | scanner.close(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Lecture 2 Codes Array String and Methods/ArrayMethods.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class ArrayMethods { 4 | public static void main(String[] args) { 5 | // Sorting an array 6 | int[] numbers = {5, 2, 8, 1, 7}; 7 | System.out.println("Original array: " + Arrays.toString(numbers)); 8 | Arrays.sort(numbers); 9 | System.out.println("Sorted array: " + Arrays.toString(numbers)); 10 | 11 | // Searching in a sorted array 12 | int searchElement = 7; 13 | int index = Arrays.binarySearch(numbers, searchElement); 14 | System.out.println("Index of " + searchElement + ": " + index); 15 | 16 | // Filling an array 17 | int[] filledArray = new int[5]; 18 | Arrays.fill(filledArray, 42); 19 | System.out.println("Filled array: " + Arrays.toString(filledArray)); 20 | 21 | // Converting array to a string 22 | String[] stringArray = {"apple", "banana", "orange"}; 23 | System.out.println("String array: " + Arrays.toString(stringArray)); 24 | 25 | // Checking array equality 26 | int[] array1 = {1, 2, 3}; 27 | int[] array2 = {1, 2, 3}; 28 | boolean arraysEqual = Arrays.equals(array1, array2); 29 | System.out.println("Arrays equal? " + arraysEqual); 30 | 31 | // Copying arrays 32 | int[] originalArray = {10, 20, 30, 40, 50}; 33 | int[] copiedArray = Arrays.copyOf(originalArray, 3); 34 | System.out.println("Copied array: " + Arrays.toString(copiedArray)); 35 | 36 | // Hash code of an array 37 | int[] hashArray = {3, 7, 1, 4, 2}; 38 | int arrayHashCode = Arrays.hashCode(hashArray); 39 | System.out.println("Hash code of the array: " + arrayHashCode); 40 | } 41 | } -------------------------------------------------------------------------------- /MP2/group007-MP2/Item4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item4 { 4 | 5 | public static void main (String[] args) { 6 | 7 | Scanner objScanner = new Scanner(System.in); 8 | System.out.print("Enter a number: "); 9 | int number = objScanner.nextInt(); 10 | if (number == 0) { // Zero is automatically not a happy number 11 | System.out.println("0 is not a happy number"); 12 | } 13 | 14 | int originalNumber = number; 15 | int searchResult = -1; 16 | int sumOfSquareDigits = 0;; 17 | int[] nums = new int[100]; 18 | nums[0] = number; 19 | 20 | // Stores all sum of square digits in an array and then stops when it finds a repeat element 21 | int i = 1; 22 | while (sumOfSquareDigits != 1 && searchResult < 0) { 23 | sumOfSquareDigits = 0; 24 | while (number > 0) { 25 | sumOfSquareDigits += Math.pow((number % 10), 2); 26 | number = number / 10; 27 | } 28 | 29 | Arrays.sort(nums); 30 | searchResult = Arrays.binarySearch(nums, sumOfSquareDigits); 31 | nums[i] = sumOfSquareDigits; 32 | number = sumOfSquareDigits; 33 | i++; 34 | } 35 | 36 | if (sumOfSquareDigits == 1){ 37 | System.out.print(originalNumber + " is a happy number!"); 38 | } 39 | else if (searchResult >= 0){ 40 | System.out.print(originalNumber + " is not a happy number"); 41 | } 42 | 43 | objScanner.close(); 44 | 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /MP2/MP2-G6/Item5.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | 5 | public class Item5{ 6 | //implementation using Strings 7 | static boolean isKarprekar(int intNumber){ 8 | //Parse square of number into string for further processing 9 | String stringNumSquared = String.valueOf(intNumber*intNumber); 10 | int numSquaredLength = stringNumSquared.length(); 11 | 12 | //Evenly splitting stringNumSquare, parsing and storing 13 | int halfStringIndex = numSquaredLength/2; 14 | int intLeftHalf = Integer.parseInt(stringNumSquared.substring(0, halfStringIndex)); 15 | int intRightHalf = Integer.parseInt(stringNumSquared.substring(halfStringIndex)); 16 | 17 | //boolean to be returned 18 | boolean boolIsKarprekar = intLeftHalf + intRightHalf == intNumber; 19 | return boolIsKarprekar; 20 | } 21 | static void checkKarprekar(int intNumber){ 22 | if(isKarprekar(intNumber)){ 23 | System.out.println("Number " + intNumber + " is a Karprekar Number"); 24 | } else { 25 | System.out.println("Number " + intNumber + " is not a Karprekar Number"); 26 | } 27 | } 28 | public static void main(String[] args) { 29 | Scanner scanner = new Scanner(System.in); 30 | System.out.print("||========||Karprekar Checker||========||\nEnter a number: "); 31 | if(scanner.hasNextInt()){ 32 | int intNumber = scanner.nextInt(); 33 | checkKarprekar(intNumber); 34 | } else { 35 | System.out.println("Input is not valid"); 36 | } 37 | System.out.println("||=====================================||"); 38 | 39 | } 40 | } -------------------------------------------------------------------------------- /MP1/Group10.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ========================================================== 3 | * Machine Problem 1 - Workspace for Group 10 4 | * ========================================================== 5 | * 6 | * Instructions: 7 | * - This file is your workspace for solving Machine Problem 1. 8 | * - You will practice the Programming Core Concepts discussed in class, 9 | * such as variables, data types, operators, and basic string operations. 10 | * - Do NOT create additional classes or advanced features (like arrays) 11 | * unless instructed. 12 | * ========================================================== 13 | */ 14 | 15 | public class Group10 { 16 | public static final int INT_RANGE_MIN = 100; 17 | public static final int INT_RANGE_MAX = 999; 18 | 19 | public static boolean isArmstrong(int intN) { 20 | int intTemp = intN; 21 | int intDigitCount = 0; 22 | int intPowSum = 0; 23 | 24 | while (intTemp > 0) { 25 | intTemp /= 10; 26 | intDigitCount++; 27 | } 28 | intTemp = intN; 29 | 30 | while (intTemp > 0) { 31 | int intDigit = intTemp % 10; 32 | intPowSum += (int)Math.pow(intDigit, intDigitCount); 33 | intTemp /= 10; 34 | } 35 | 36 | return intN == intPowSum; 37 | } 38 | 39 | public static void main(String[] args) { 40 | int intSum = 0; 41 | System.out.printf("Armstrong numbers from %d to %d:\n", INT_RANGE_MIN, INT_RANGE_MAX); 42 | for (int intI = INT_RANGE_MIN; intI <= INT_RANGE_MAX; intI++) { 43 | if (isArmstrong(intI)) { 44 | System.out.printf("%d ", intI); 45 | intSum += intI; 46 | } 47 | } 48 | System.out.printf("\nSum of Armstrong numbers from %d to %d: %d\n", INT_RANGE_MIN, INT_RANGE_MAX, intSum); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /MP2/MP-G1/Item15.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /*"Frequency of Elements in an Array" 4 | Write a program that accepts n numbers into an array and counts how many times each number 5 | appears. Display the frequency of each element.*/ 6 | 7 | public class Item15 { 8 | public static void main(String[] args) { 9 | Scanner sc = new Scanner(System.in); 10 | System.out.println("FREQUENCY NUMBER GENERATOR"); 11 | System.out.print("Enter number of elements: "); 12 | while (!sc.hasNextInt()) { 13 | System.out.print("\tInvalid.\n\tPlease enter a valid number: "); 14 | sc.next(); 15 | } 16 | int intNum = sc.nextInt(); 17 | int[] intArr = new int[intNum]; 18 | boolean[] boolVisited = new boolean[intNum]; 19 | 20 | System.out.println("{Enter elements}"); 21 | for (int i = 0; i < intNum; i++){ 22 | System.out.printf("Element %d: ", i+1); 23 | while (!sc.hasNextInt()) { 24 | System.out.print("\tInvalid.\n\tPlease enter a valid number: "); 25 | sc.next(); 26 | } 27 | intArr[i] = sc.nextInt(); 28 | } 29 | 30 | for (int i = 0; i < intNum; i++) { 31 | if (boolVisited[i]) 32 | continue; 33 | 34 | int intCount = 1; 35 | for (int j = i + 1; j < intNum; j++) { 36 | if (intArr[i] == intArr[j]) { 37 | boolVisited[j] = true; 38 | intCount++; 39 | } 40 | } 41 | System.out.println(intArr[i] + " appears " + intCount + " times"); 42 | } 43 | sc.close(); 44 | System.out.println("Program Ended Succesfully."); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /MP2/MP-G4/Item12.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | 5 | public class Item12 { 6 | public static void main (String args[]) throws IOException { 7 | 8 | // Create a BufferedReader to read input from the user 9 | BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); 10 | 11 | // Prompt the user to enter a sentence 12 | System.out.print("Enter a sentence: "); 13 | String sentence = inputReader.readLine(); // Read the entire line of input 14 | 15 | // Remove any leading or trailing spaces from the sentence 16 | sentence = sentence.trim(); 17 | 18 | // Get the total number of characters in the sentence 19 | int sentenceLength = sentence.length(); 20 | 21 | // Initialize word counter 22 | int wordCount = 0; 23 | 24 | // Loop through each character in the sentence 25 | // This loop should start at i = 1 (not 0), 26 | // because at i = 0, sentence.charAt(i - 1) would mean charAt(-1), which is invalid. 27 | for (int i = 0; i < sentenceLength; i++) { 28 | 29 | // If current character is a space AND the previous character is not a space, 30 | // that means a word has just ended, so increment the counter 31 | if (sentence.charAt(i) == ' ' && sentence.charAt(i - 1) != ' ') { 32 | 33 | wordCount = wordCount + 1; 34 | } 35 | } 36 | 37 | // Add 1 to account for the last word (not followed by a space) 38 | wordCount = wordCount + 1; 39 | 40 | // Display the final word count 41 | System.out.println("Here is the total amount of words in the sentence: " + wordCount); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /MP2/group007-MP2/Item15.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item15 { 4 | public static void main (String[] args) { 5 | Scanner scan = new Scanner(System.in); 6 | 7 | System.out.print("Enter the size of an array: "); 8 | int arraySize = scan.nextInt(); 9 | if (arraySize <= 0) { 10 | System.out.println("The size of an array must be greater than 0."); 11 | System.exit(0); 12 | } 13 | 14 | int[] Array = Input(arraySize, scan); 15 | 16 | Map Map = new HashMap(); 17 | 18 | int currentIndex = 0; 19 | int intCounter = 0; 20 | 21 | CountsTheRepetitions(Array, Map, currentIndex, intCounter, arraySize); 22 | 23 | System.out.println("Number of times each element is repeated: "); 24 | for (Map.Entry entry : Map.entrySet()) { 25 | System.out.println(entry.getKey() + ": " + entry.getValue()); 26 | } 27 | 28 | scan.close(); 29 | 30 | } 31 | 32 | static int[] Input(int arraySize, Scanner scan) { 33 | int[] ArrayToReturn = new int[arraySize]; 34 | for (int i = 0; i < arraySize; i++) { 35 | System.out.printf("Enter an integer at index %d: ", i); 36 | ArrayToReturn[i] = scan.nextInt(); 37 | } 38 | return ArrayToReturn; 39 | } 40 | 41 | static void CountsTheRepetitions(int[] Array, Map Map, int currentIndex, int intCounter, int arraySize) { 42 | for (int i = 0; i < arraySize; i++) { 43 | currentIndex = Array[i]; 44 | intCounter = 0; 45 | intCounter = Map.getOrDefault(currentIndex, 0); 46 | Map.put(currentIndex, intCounter + 1); 47 | } 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /MP2/MP2-G10/Item15.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Item15 { 5 | public static void main(String[] args) throws IOException { 6 | BufferedReader buffread = new BufferedReader(new InputStreamReader(System.in)); 7 | 8 | int intArrSize = 0; 9 | while (true) { 10 | System.out.print("Enter Number of Elements: "); 11 | 12 | try { 13 | intArrSize = Integer.parseInt(buffread.readLine()); 14 | } 15 | catch (NumberFormatException e) { 16 | System.out.println("Please input valid integer"); 17 | } 18 | 19 | if (intArrSize > 0) { 20 | break; 21 | } 22 | } 23 | 24 | int[] intNumArr = null; 25 | while (true) { 26 | System.out.printf("Enter %d elements:\n", intArrSize); 27 | 28 | String[] intInputArr = buffread.readLine().split(" "); 29 | intNumArr = new int[intArrSize]; 30 | 31 | try { 32 | for (int intI = 0; intI < intArrSize; intI++) { 33 | intNumArr[intI] = Integer.parseInt(intInputArr[intI]); 34 | } 35 | } 36 | catch (NumberFormatException e) { 37 | System.out.println("Please input valid integers"); 38 | continue; 39 | } 40 | break; 41 | } 42 | 43 | 44 | Arrays.sort(intNumArr); 45 | 46 | System.out.println("Frequency Count:"); 47 | int intCurrFreq = 1; 48 | for (int intI = 1; intI < intArrSize; intI++) { 49 | if (intNumArr[intI] == intNumArr[intI - 1]) { 50 | intCurrFreq++; 51 | } 52 | else { 53 | System.out.printf("%d appears %d time/s\n", intNumArr[intI - 1], intCurrFreq); 54 | intCurrFreq = 1; 55 | } 56 | } 57 | // ugly... just so that its not O(n^2) 58 | System.out.printf("%d appears %d times/s\n", intNumArr[intArrSize - 1], intCurrFreq); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /MP2/MP-G1/Item8.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item8{ 4 | public static void main (String[] args){ 5 | Scanner scanner = new Scanner(System.in); //Scanner object as always 6 | int intNumber = 0; 7 | boolean correctInput = false; //The correct input is false first 8 | 9 | while(!correctInput){ 10 | System.out.print("Please enter a positive integer: "); 11 | try { 12 | intNumber = scanner.nextInt(); 13 | if(intNumber < 0){ 14 | System.out.println("Please enter a positive integer again."); 15 | } else { 16 | correctInput = true;//Input must be correct here 17 | } 18 | } catch (Exception e) { 19 | System.out.println("Invalid input. Please enter a positive number."); 20 | scanner.next();//Prevents infinite loop. 21 | } 22 | } 23 | 24 | if(isAutomorphic(intNumber)){ 25 | System.out.println("This number is automorphic.");//Showing this is automorphic 26 | } else { 27 | System.out.println("This number is NOT automorphic.");//If the input is not an automorphic number. 28 | } 29 | 30 | scanner.close(); 31 | } 32 | //Boolean function to check if it is automorphic or not 33 | public static boolean isAutomorphic(int intNum){ 34 | long square = (long) intNum * intNum; //Finds the square of the number. 35 | //Converts both the number and its square to compare 36 | String numString = String.valueOf(intNum); 37 | String squareString = String.valueOf(square); 38 | //Checks if the square string ends with the number 39 | return squareString.endsWith(numString); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /MP2/MP2-G5/Item4.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class Item4 { 5 | public static void main (String args[]){ 6 | Scanner scanner = new Scanner (System.in); 7 | 8 | System.out.print("enter a number:"); 9 | int myNumber = scanner.nextInt(); 10 | 11 | boolean answer = isHappyNumber(myNumber); 12 | 13 | if (answer == true){ 14 | System.out.println(myNumber + " is a happy number :)"); 15 | } else { 16 | System.out.println(myNumber + " is not a happy number :("); 17 | } 18 | 19 | scanner.close(); 20 | } 21 | 22 | public static boolean isHappyNumber(int number){ 23 | int[] previousSums = new int[1]; 24 | int count = 0; 25 | int tempNumber = number; 26 | 27 | while (true) { 28 | int sum = sumOfSquares(tempNumber); 29 | 30 | if (sum == 1) { 31 | return true; 32 | } 33 | 34 | for (int i = 0; i < count; i++) { 35 | if (previousSums[i] == sum) { 36 | return false; 37 | } 38 | } 39 | 40 | if (count == previousSums.length) { 41 | previousSums = Arrays.copyOf(previousSums, previousSums.length + 10); 42 | } 43 | 44 | previousSums[count] = sum; 45 | count++; 46 | tempNumber = sum; 47 | } 48 | } 49 | 50 | public static int sumOfSquares(int number){ 51 | int digitSquare; 52 | int tempNumber = number; 53 | int sumOfDigitSquares = 0; 54 | 55 | while (tempNumber > 0){ 56 | digitSquare = tempNumber % 10; 57 | 58 | digitSquare *= digitSquare; 59 | sumOfDigitSquares += digitSquare; 60 | 61 | tempNumber /= 10; 62 | } 63 | return sumOfDigitSquares; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /MP2/MP-G1/Item2.java: -------------------------------------------------------------------------------- 1 | // Item 2: The program accepts a positive integer input and checks whether a given number is a Harshad Number. A Harshad Number is an integer that is divisible by the sum of its digits. 2 | 3 | import java.util.*; 4 | 5 | public class Item2 { 6 | public static void main(String[] args) { 7 | Scanner scanner = new Scanner(System.in); 8 | int intNumber = -1; 9 | 10 | System.out.print("\n\n\n\tGroup 1 - Item 2: Harshad Number"); 11 | 12 | while(intNumber <= 0) { // Asks for and validates user input 13 | System.out.print("\n\n\tEnter a positive integer: "); 14 | if(scanner.hasNextInt()) { 15 | intNumber = scanner.nextInt(); 16 | 17 | if(intNumber <= 0) { 18 | System.out.print("\tInvalid input."); 19 | scanner.nextLine(); 20 | } 21 | } else { 22 | System.out.print("\tInvalid input."); 23 | scanner.nextLine(); 24 | } 25 | } 26 | 27 | if (intNumber % sumDigits(intNumber) == 0) { // Calls the sumDigits method and checks if the input is a Harshad Number; Prints the result 28 | System.out.printf("\n\n\n\t%d is a Harshad Number!", intNumber); 29 | } else { 30 | System.out.printf("\n\n\n\t%d is not a Harshad Number.", intNumber); 31 | } 32 | System.out.println("\n\n\tThank you for using this program! Exiting..."); 33 | 34 | scanner.close(); 35 | } 36 | 37 | public static int sumDigits(int intNumber) { // Used to calculate the sum of the digits of the input 38 | int intDigit, intSum = 0; 39 | 40 | while(intNumber > 0) { 41 | intDigit = intNumber % 10; 42 | intSum += intDigit; 43 | intNumber /= 10; 44 | } 45 | 46 | return intSum; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /MP2/MP2-G10/Item4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item4 { 4 | public static int getSumOfSquares(int num) { 5 | int intSum = 0; 6 | while (num > 0) { 7 | int intDigit = num % 10; 8 | intSum += (intDigit * intDigit); 9 | num /= 10; 10 | } 11 | return intSum; 12 | } 13 | 14 | public static boolean isHappyNumber(int intN) { 15 | int intCount = 0; 16 | 17 | while (intN != 1 && intCount < 100) { 18 | intN = getSumOfSquares(intN); 19 | intCount++; 20 | } 21 | 22 | return intN == 1; 23 | } 24 | 25 | public static void main(String[] args) { 26 | 27 | System.out.println("This program checks if a number is a Happy Number."); 28 | Scanner sc = new Scanner(System.in); 29 | String strChoice; 30 | 31 | do { 32 | int intNum = 0; 33 | boolean boolValidInput = false; 34 | 35 | while (!boolValidInput) { 36 | System.out.print("Enter a number: "); 37 | if (sc.hasNextInt()) { 38 | intNum = sc.nextInt(); 39 | boolValidInput = true; 40 | } else { 41 | System.out.println("Invalid input! Please enter an integer."); 42 | sc.next(); 43 | } 44 | } 45 | 46 | if (isHappyNumber(intNum)) { 47 | System.out.println(intNum + " is a Happy Number!"); 48 | } else { 49 | System.out.println(intNum + " is not a Happy Number."); 50 | } 51 | 52 | System.out.print("Do you want to check another number? (Y/N): "); 53 | strChoice = sc.next().trim().toUpperCase(); 54 | 55 | while (!strChoice.equals("Y") && !strChoice.equals("N")) { 56 | System.out.print("Invalid choice! Please enter Y or N: "); 57 | strChoice = sc.next().trim().toUpperCase(); 58 | } 59 | 60 | } while (strChoice.equals("Y")); 61 | 62 | System.out.println("Program exited."); 63 | sc.close(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Lecture 3 OOP Concepts/Encapsulation/BasedOnAccessModifier.java: -------------------------------------------------------------------------------- 1 | // Base class with all access levels 2 | class ExampleData { 3 | private int privateData = 10; // Only within ExampleData 4 | protected int protectedData = 20; // Same package + subclasses 5 | public int publicData = 30; // Accessible everywhere 6 | int defaultData = 40; // Package-private (same package only) 7 | 8 | // Getter for privateData 9 | public int getPrivateData() { 10 | return privateData; 11 | } 12 | } 13 | 14 | // Subclass extending ExampleData 15 | class ChildData extends ExampleData { 16 | public void showData() { 17 | // System.out.println("Private Data: " + privateData); ❌ Not allowed 18 | System.out.println("Protected Data (inherited): " + protectedData); // ✅ Allowed 19 | System.out.println("Public Data (inherited): " + publicData); // ✅ Allowed 20 | System.out.println("Default Data (inherited): " + defaultData); // ✅ Allowed (same package) 21 | } 22 | } 23 | 24 | // Main class to run the program 25 | public class BasedOnAccessModifier { 26 | public static void main(String[] args) { 27 | ExampleData data = new ExampleData(); 28 | ChildData child = new ChildData(); 29 | 30 | // ✅ Public data is directly accessible 31 | System.out.println("Public Data: " + data.publicData); 32 | 33 | // ✅ Private data via getter 34 | System.out.println("Private Data (via getter): " + data.getPrivateData()); 35 | 36 | // ✅ Default data works here (same package) 37 | System.out.println("Default Data: " + data.defaultData); 38 | 39 | // ✅ Protected data works here (same package) 40 | System.out.println("Protected Data: " + data.protectedData); 41 | 42 | System.out.println("\n--- From Subclass (ChildData) ---"); 43 | child.showData(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /MP2/MP-G9/Item7.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item7 { 4 | public static boolean checkPrime(int intNumber) { 5 | if (intNumber <= 1) 6 | return false; 7 | 8 | for (int i = 2; i <= intNumber/2; i++) { 9 | if (intNumber % i == 0) 10 | return false; 11 | } 12 | return true; 13 | } 14 | 15 | public static void main(String[] args) { 16 | Scanner scrInput = new Scanner(System.in); 17 | int intminRange; 18 | int intmaxRange; 19 | int intRangechecker =0; 20 | do{ 21 | System.out.print("Enter a minimum range: "); 22 | while (!scrInput.hasNextInt()) { 23 | System.out.println("Invalid input! Please enter an integer."); 24 | scrInput.next(); 25 | System.out.print("Enter a minimum range: "); 26 | } 27 | intminRange = scrInput.nextInt(); 28 | System.out.print("Enter a maximum range: "); 29 | while (!scrInput.hasNextInt()) { 30 | System.out.println("Invalid input! Please enter an integer."); 31 | scrInput.next(); 32 | System.out.print("Enter a maximum range: "); 33 | } 34 | intmaxRange = scrInput.nextInt(); 35 | 36 | if(intminRange > intmaxRange){ 37 | System.out.println("Minimum range cannot be greater than maximum range. Try again!"); 38 | } 39 | }while(intminRange > intmaxRange); 40 | 41 | for (int i = intminRange; i < intmaxRange; i++) { 42 | if (checkPrime(i) && checkPrime(i + 2)) { 43 | System.out.print("(" + i + "," + (i + 2) + ") "); 44 | intRangechecker++; 45 | } 46 | } 47 | 48 | if(intRangechecker == 0){ 49 | System.out.println("No Twin Prime Exists within the Range"); 50 | } 51 | scrInput.close(); 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /MP2/MP2-G11/Item14.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item14 { 4 | public static void main(String[] args) { 5 | Scanner console = new Scanner(System.in); 6 | int[][] matrix, transpose; 7 | int rows = getSize(console, "row"); 8 | int cols = getSize(console, "column"); 9 | 10 | matrix = new int[rows][cols]; 11 | inputMatrix(console, matrix, "Matrix"); 12 | 13 | System.out.println("\nOriginal Matrix:"); 14 | displayMatrix(matrix); 15 | 16 | transpose = transposeMatrix(matrix); 17 | System.out.println("\nTransposed Matrix:"); 18 | displayMatrix(transpose); 19 | 20 | console.close(); 21 | } 22 | 23 | static int getSize(Scanner console, String type) { 24 | System.out.print("Enter the size of the " + type + ": "); 25 | return console.nextInt(); 26 | } 27 | 28 | static void inputMatrix(Scanner console, int[][] mat, String name) { 29 | for (int i = 0; i < mat.length; i++) { 30 | for (int j = 0; j < mat[i].length; j++) { 31 | System.out.print("Enter " + name + "[" + (i + 1) + "][" + (j + 1) + "]: "); 32 | mat[i][j] = console.nextInt(); 33 | } 34 | } 35 | } 36 | 37 | static int[][] transposeMatrix(int[][] mat) { 38 | int rows = mat.length; 39 | int cols = mat[0].length; 40 | int[][] transposed = new int[cols][rows]; 41 | for (int i = 0; i < rows; i++) { 42 | for (int j = 0; j < cols; j++) { 43 | transposed[j][i] = mat[i][j]; 44 | } 45 | } 46 | return transposed; 47 | } 48 | 49 | static void displayMatrix(int[][] mat) { 50 | for (int[] row : mat) { 51 | for (int val : row) { 52 | System.out.printf("[%4d] ", val); 53 | } 54 | System.out.println(); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /MP2/MP-G1/Item4.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /*"Happy Number" 4 | Create a program that checks if a number is a Happy Number. 5 | A Happy Number is a number that eventually reaches 1 when repeatedly replaced by the sum of the squares of its digits. 6 | If it loops endlessly, it is not a Happy Number.*/ 7 | 8 | public class Item4 { 9 | public static void main(String[] args) { 10 | Scanner sc = new Scanner(System.in); 11 | System.out.println("HAPPY NUMBER CHECKER"); 12 | int intNum; 13 | 14 | while (true) { 15 | System.out.print("Enter a number: "); 16 | if (!sc.hasNextInt()) { 17 | System.out.print("\tInvalid.\n\tPlease enter a valid integer.\n"); 18 | sc.next(); // discard invalid input 19 | continue; 20 | } 21 | intNum = sc.nextInt(); 22 | 23 | if (intNum <= 0) { 24 | System.out.print("\tInvalid.\n\tPlease enter a positive integer only.\n"); 25 | continue; 26 | } 27 | break; // valid positive integer 28 | } 29 | 30 | int intResult = intNum; 31 | while (intResult != 1 && intResult != 4) { 32 | intResult = sumOfSquares(intResult); 33 | } 34 | 35 | if (intResult == 1) { 36 | System.out.println(intNum + " is a Happy Number!"); 37 | } else { 38 | System.out.println(intNum + " is NOT a Happy Number."); 39 | } 40 | sc.close(); 41 | System.out.println("Program Ended Succesfully."); 42 | } 43 | 44 | // Method to compute sum of squares of digits 45 | public static int sumOfSquares(int intNum) { 46 | int intSum = 0; 47 | while (intNum > 0) { 48 | int intDigit = intNum % 10; 49 | intSum += intDigit * intDigit; 50 | intNum /= 10; 51 | } 52 | return intSum; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /MP2/MP2-G5/Item13.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item13 { 4 | static int rows = 4; 5 | static int columns = 4; 6 | public static void main(String args[]){ 7 | Scanner scanner = new Scanner(System.in); 8 | 9 | int [][] matrixA = new int [rows][columns]; 10 | int [][] matrixB = new int [rows][columns]; 11 | 12 | matrixInput(scanner, matrixA, "first"); 13 | matrixInput(scanner, matrixB, "second"); 14 | 15 | System.out.println("The first matrix contains:"); 16 | displayMatrix(matrixA); 17 | System.out.println("The second matrix contains:"); 18 | displayMatrix(matrixB); 19 | 20 | System.out.println("Product"); 21 | int [][] result = multiplyMatrices(matrixA, matrixB); 22 | displayMatrix(result); 23 | 24 | scanner.close(); 25 | } 26 | 27 | public static void matrixInput (Scanner scanner, int matrix[][], String name){ 28 | System.out.println("Enter the numbers for the " + name + "matrix:"); 29 | for (int i = 0; i < rows; i++){ 30 | for (int j = 0; j < columns; j++){ 31 | matrix[i][j] = scanner.nextInt(); 32 | } 33 | } 34 | } 35 | 36 | public static int[][] multiplyMatrices(int[][] matrixA, int[][] matrixB){ 37 | int [][] result = new int[rows][columns]; 38 | 39 | for (int i = 0; i < rows; i++) { 40 | for (int j = 0; j < columns; j++) { 41 | for (int k = 0; k < columns; k++) { 42 | result[i][j] += matrixA[i][k] * matrixB[k][j]; 43 | } 44 | } 45 | } 46 | return result; 47 | } 48 | 49 | public static void displayMatrix(int[][] matrix) { 50 | for (int i = 0; i < rows; i++) { 51 | for (int j = 0; j < columns; j++) { 52 | System.out.print(matrix[i][j] + " "); 53 | } 54 | System.out.println(); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /MP2/group007-MP2/Item10.java: -------------------------------------------------------------------------------- 1 | import java.util.InputMismatchException; 2 | import java.util.Scanner; 3 | 4 | public class AnagramChecker { 5 | public static void main(String[] args) { 6 | try { 7 | Scanner input = new Scanner(System.in); 8 | System.out.println("Input two words to check if its an Anagram."); 9 | System.out.print("First word: "); 10 | String firstWord = input.next(); 11 | char[] firstWordArray = firstWord.toCharArray(); 12 | System.out.print("Second word: "); 13 | String secondWord = input.next(); 14 | char[] secondWordArray = secondWord.toCharArray(); 15 | int anagramCheck = 0; 16 | if (firstWordArray.length == secondWordArray.length) { 17 | boolean[] used = new boolean[secondWordArray.length]; 18 | for (int i = 0; i < firstWordArray.length; i++) { 19 | for (int j = 0; j < secondWordArray.length; j++) { 20 | if (firstWordArray[i] == secondWordArray[j] && !used[j]) { 21 | anagramCheck++; 22 | used[j] = true; 23 | break; 24 | } 25 | } 26 | } 27 | if (anagramCheck == firstWordArray.length) { 28 | System.out.print("These two words are an anagram."); 29 | } else { 30 | System.out.print("These two words are not an anagram."); 31 | } 32 | } else { 33 | System.out.print("These two words are not an anagram."); 34 | } 35 | input.close(); 36 | } catch (InputMismatchException e) { 37 | System.out.println("Invalid input! Please enter valid words only."); 38 | } catch (Exception e) { 39 | System.out.println("An error occurred: " + e.getMessage()); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /MP2/MP2-G6/Item11.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item11 { 4 | 5 | public static void vowelAndConsonantCounter(String strInput) { 6 | int intVowelsCount = 0; 7 | int intConsonantsCount = 0; 8 | 9 | strInput = strInput.toLowerCase(); 10 | 11 | for (int loopStart = 0; loopStart < strInput.length(); loopStart++) { 12 | char charInputted= strInput.charAt(loopStart); 13 | 14 | if (charInputted >= 'a' && charInputted <= 'z') { 15 | if (charInputted == 'a' || charInputted == 'e' || charInputted == 'i' || charInputted == 'o' || charInputted == 'u') { 16 | intVowelsCount++; 17 | } else { 18 | intConsonantsCount++; 19 | } 20 | } 21 | } 22 | 23 | System.out.println("Number of vowels: " + intVowelsCount); 24 | System.out.println("Number of consonants: " + intConsonantsCount); 25 | } 26 | 27 | public static boolean inputChecker (String strInput) { 28 | for (int loopStart = 0; loopStart < strInput.length(); loopStart++) { 29 | char charInputted= strInput.charAt(loopStart); 30 | if (charInputted >= 'a' && charInputted <= 'z') { 31 | continue; 32 | } else if (charInputted == ' ') { 33 | continue; 34 | } else { 35 | return false; 36 | } 37 | 38 | } 39 | return true; 40 | } 41 | 42 | public static void main(String args[]) { 43 | Scanner scanner = new Scanner(System.in); 44 | 45 | System.out.print("Enter a string: "); 46 | String strInput = scanner.nextLine(); 47 | 48 | if (inputChecker(strInput)) { 49 | vowelAndConsonantCounter(strInput); 50 | } else { 51 | System.out.println("Please enter a word or letters only! :> "); 52 | } 53 | scanner.close(); 54 | } 55 | } 56 | 57 | 58 | -------------------------------------------------------------------------------- /MP2/MP2-G6/Item7.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item7 { 4 | //checks for whole numbers 5 | private static int getWholeNumber(Scanner numberScanner, String prompt) { 6 | //loops until whole number found 7 | while (true) { 8 | System.out.print(prompt); 9 | if (numberScanner.hasNextInt()) { 10 | return numberScanner.nextInt(); //continue with number entered 11 | } else { 12 | System.out.println("Error. Please enter a whole number."); 13 | numberScanner.next(); 14 | } 15 | } 16 | } 17 | 18 | //checks for prime numbers 19 | private static boolean getPrimeNumber(int num) { 20 | //false for negatives, 0, and 1 21 | if (num <= 1) { 22 | return false; 23 | } 24 | 25 | //for loop starting from 2 and ending with certain number 26 | for (int i = 2; i * i <= num; i++) { 27 | //false for numbers divisible by 2 28 | if (num % i == 0) { 29 | return false; 30 | } 31 | } 32 | 33 | return true; 34 | } 35 | 36 | public static void main(String args[]) { 37 | Scanner numberScanner = new Scanner(System.in); 38 | 39 | int rangeStart = getWholeNumber(numberScanner, "Enter start of range: "); 40 | int rangeEnd = getWholeNumber(numberScanner, "Enter end of range: "); 41 | numberScanner.close(); 42 | 43 | boolean isFirstPair = true; 44 | for (int num = rangeStart; num <= rangeEnd; num++) { 45 | if (getPrimeNumber(num) && getPrimeNumber(num + 2)) { 46 | //each iteration starts with a comma after the first pair 47 | if (!isFirstPair) { 48 | System.out.print(", "); 49 | } 50 | System.out.print("(" + num + ", " + (num + 2) + ")"); 51 | isFirstPair = false; 52 | } 53 | } 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /MP2/MP2-G3/Item8.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 8. Automorphic Number 3 | * Create a program to check if a number is 4 | * an Automorphic Number. A number is Automorphic if its 5 | * square ends with the number itself (e.g., 76^2 = 5776). 6 | */ 7 | 8 | import java.util.Scanner; 9 | 10 | public class Item8 { 11 | static boolean isAutomorphic(int number){ 12 | int square = number * number; 13 | 14 | int checker = 1; 15 | int extractNumber, extractSquare; 16 | 17 | int temp = number; 18 | 19 | while(temp != 0){ 20 | // to extract digit 21 | extractNumber = number % 10; 22 | extractSquare = square % 10; 23 | 24 | // checks if the extracted numbers are similar, if not, var checker will equal to 0 25 | if(extractNumber != extractSquare){ 26 | checker = 0; 27 | break; 28 | } 29 | 30 | // to remove last digit 31 | number = number /10; 32 | square = square / 10; 33 | temp = temp / 10; 34 | } 35 | 36 | // if the checker is still 1, then the ends are similar 37 | return (checker == 1); 38 | 39 | // it's ugly, yet it works ;-; ; 40 | } 41 | 42 | public static void main(String[] args) { 43 | Scanner scanner = new Scanner(System.in); 44 | 45 | System.out.print("Enter a number: "); 46 | 47 | //Ensure only integer input 48 | while (!scanner.hasNextInt()) { 49 | System.out.println("Invalid input! Please enter an integer."); 50 | System.out.print("Enter a number: "); 51 | scanner.next(); // clear invalid input 52 | } 53 | 54 | int number = scanner.nextInt(); 55 | 56 | if (isAutomorphic(number)) { 57 | System.out.println(number + " is an Automorphic Number!"); 58 | } else { 59 | System.out.println(number + " is NOT an Automorphic Number."); 60 | } 61 | 62 | scanner.close(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /MP2/MP-G1/Item6.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /* Machine Problem 2 Programming Concepts 4 | * Item 6 5 | * Ask the user to enter a number and display all of its prime factors. 6 | * For example, the prime factors of 84 are 2,2,3,7 7 | */ 8 | 9 | public class Item6 { 10 | public static void main(String[] args) { 11 | Scanner scanner = new Scanner(System.in); 12 | 13 | //Input of a number to be factored 14 | int number = inputNum(scanner, "Enter a positive number: "); 15 | 16 | System.out.print("The prime factors of " + number + " are: "); 17 | 18 | // Factor out the 2s first 19 | while (number % 2 == 0) { 20 | System.out.print(2 + " "); 21 | number /= 2; 22 | } 23 | 24 | // Factor out odd numbers starting from 3 25 | for (int i = 3; i <= number; i += 2) { 26 | while (number % i == 0) { 27 | System.out.print(i + " "); 28 | number /= i; 29 | } 30 | } 31 | 32 | // If a number is still greater than 2, it's a prime factor 33 | if (number > 2) { 34 | System.out.print(number); 35 | } 36 | 37 | scanner.close(); 38 | } 39 | // Method for input and error handling 40 | public static int inputNum (Scanner scanner, String text) { 41 | int value = -1; 42 | while (value <= 0) { 43 | System.out.print(text); 44 | if (scanner.hasNextInt()) { 45 | value = scanner.nextInt(); 46 | if (value <= 0) { 47 | System.out.println("Invalid input. Enter a number greater than zero."); 48 | } 49 | } 50 | else { 51 | System.out.println("Invalid input. Enter a valid postive number."); 52 | scanner.next(); 53 | } 54 | } 55 | return value; 56 | } 57 | } 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /MP1/Group11.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ========================================================== 3 | * Machine Problem 1 - Workspace for Group 11 4 | * ========================================================== 5 | * 6 | * Instructions: 7 | * - This file is your workspace for solving Machine Problem 1. 8 | * - You will practice the Programming Core Concepts discussed in class, 9 | * such as variables, data types, operators, and basic string operations. 10 | * - Do NOT create additional classes or advanced features (like arrays) 11 | * unless instructed. 12 | * ========================================================== 13 | */ 14 | 15 | public class Group11 { 16 | public static void main(String[] args) { 17 | 18 | 19 | int intPrimeCount = 0; 20 | int intLargestPrime = 0; 21 | int intPrimeSum = 0; 22 | 23 | System.out.print("Prime Numbers (1-100): "); 24 | 25 | for(int i = 1; i <= 100; i++){ 26 | boolean boolIsPrime = true; // Assume number is a prime 27 | 28 | if(i <= 1){ //Checks if number is less than or equal to 1 29 | boolIsPrime = false; //If so the number is not a prime 30 | 31 | } else { 32 | for(int j = 2; j * j <= i; j++){ //Checks 2 onwards if number is divisible by it 33 | if (i % j == 0){ 34 | boolIsPrime = false; // If so then it is not a prime 35 | break; 36 | } 37 | } 38 | } 39 | 40 | if(boolIsPrime){ //If number is prime increment count, add to sum and assume it is the largest 41 | intPrimeCount++; 42 | intPrimeSum += i; 43 | intLargestPrime = i; //Largest will keep changing until loop stops 44 | System.out.print(i + " "); //Displays to the list of prime number 45 | } 46 | } 47 | 48 | //Display numbers count, sum and the largest prime 49 | System.out.println(); 50 | System.out.printf("Prime Numbers Count: %d\n", intPrimeCount); 51 | System.out.printf("Sum of all Primes(1-100): %d\n", intPrimeSum); 52 | System.out.printf("Largest Prime: %d", intLargestPrime); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /MP2/MP2-G3/Item4.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 4. Happy Number 3 | * Create a program that checks if a number is a Happy Number. A Happy Number is a number that 4 | * eventually reaches 1 when repeatedly replaced by the sum of the squares of its digits. If it loops 5 | * endlessly, it is not a Happy Number. 6 | */ 7 | 8 | import java.util.Scanner; 9 | 10 | public class Item4 { 11 | public static void main (String[] args){ 12 | 13 | Scanner scanner = new Scanner(System.in); 14 | 15 | System.out.print("Please enter a number: "); 16 | 17 | //Ensure only integer input 18 | while (!scanner.hasNextInt()) { 19 | System.out.println("Invalid input! Please enter an integer."); 20 | System.out.print("Please enter a number: "); 21 | scanner.next(); // clear invalid input 22 | } 23 | 24 | int intNum = scanner.nextInt(); 25 | 26 | if(boolIsHappyNumber(intNum)){ 27 | System.out.println(intNum + " is a Happy Number!"); 28 | } else{ 29 | System.out.println(intNum + " is NOT a Happy Number."); 30 | } 31 | 32 | // close scanner 33 | scanner.close(); 34 | } 35 | 36 | // Method to calculate the sum of the squares of digits 37 | public static int sumOfSquares(int intNum){ 38 | int intSum = 0; 39 | while (intNum > 0 ){ 40 | int intDigit = intNum % 10; // last digit 41 | intSum += intDigit * intDigit; // square and add 42 | intNum /= 10; // remove last digit 43 | } 44 | return intSum; 45 | } 46 | 47 | // Method to check if number is 'Happy' 48 | public static boolean boolIsHappyNumber(int intNum){ 49 | int intSlow = intNum; 50 | int intFast = intNum; 51 | 52 | // Fast and Slow pointers (Floyd' Cycle Detection) 53 | do{ 54 | intSlow = sumOfSquares(intSlow); 55 | intFast = sumOfSquares(sumOfSquares(intFast)); 56 | }while (intFast != intSlow); 57 | 58 | return intSlow == 1; // If cycle ends with 1 == 'Happy' 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /MP2/MP2-G10/Item6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item6 { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | String strChoice; 7 | 8 | System.out.println("This program asks the user to enter a number and displays all its prime factors."); 9 | 10 | do { 11 | int intNum = getValidNumber(scanner); 12 | System.out.print("The Prime factors of " + intNum + " are: "); 13 | printPrimeFactors(intNum); 14 | System.out.println(); 15 | strChoice = getYesOrNo(scanner, "Do you want to try again? (Y/N): "); 16 | } while (strChoice.equalsIgnoreCase("Y")); 17 | 18 | System.out.println("Program terminated."); 19 | scanner.close(); 20 | } 21 | 22 | public static int getValidNumber(Scanner scanner) { 23 | int intN = 0; 24 | boolean boolValidInput = false; 25 | 26 | while (!boolValidInput) { 27 | System.out.print("\nEnter a number: "); 28 | 29 | if (scanner.hasNextInt()) { 30 | intN = scanner.nextInt(); 31 | if (intN > 1) { 32 | boolValidInput = true; 33 | } else { 34 | System.out.println("Please enter an integer greater than 1."); 35 | } 36 | } else { 37 | System.out.println("Invalid input! Please enter an integer."); 38 | scanner.next(); 39 | } 40 | } 41 | 42 | return intN; 43 | } 44 | 45 | public static void printPrimeFactors(int intN) { 46 | for (int intI = 2; intI <= intN; intI++) { 47 | while (intN % intI == 0) { 48 | System.out.print(intI + " "); 49 | intN /= intI; 50 | } 51 | } 52 | } 53 | 54 | public static String getYesOrNo(Scanner scanner, String strPrompt) { 55 | String strChoice; 56 | do { 57 | System.out.print(strPrompt); 58 | strChoice = scanner.next(); 59 | if (!strChoice.equalsIgnoreCase("Y") && !strChoice.equalsIgnoreCase("N")) { 60 | System.out.println("\nInvalid input! Please enter Y or N."); 61 | } 62 | } while (!strChoice.equalsIgnoreCase("Y") && !strChoice.equalsIgnoreCase("N")); 63 | 64 | return strChoice; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /MP2/MP2-G3/Item3.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3. Strong Number 3 | * Write a program that checks if a given number is a 4 | * Strong Number. A Strong Number is one where the sum 5 | * of the factorial of its digits equals the number itself 6 | * (e.g., 145 because 1! + 4! + 5! = 145). 7 | */ 8 | 9 | import java.util.InputMismatchException; 10 | import java.util.Scanner; 11 | 12 | public class Item3{ 13 | static boolean isStrongNumber(int number){ 14 | int temp=number; 15 | int sumFactorial =0; 16 | 17 | while(temp != 0){ 18 | int extractNumber = temp % 10; // extract digits 19 | 20 | int factorial = 1; // reset for each digits 21 | 22 | // calculate factorial for each digits 23 | for(int i=1; i <= extractNumber; i++){ 24 | factorial = factorial*i; 25 | } 26 | 27 | sumFactorial = sumFactorial + factorial; // store all the sum 28 | 29 | temp = temp / 10; // for removing last digit 30 | } 31 | 32 | return (sumFactorial == number); 33 | } 34 | 35 | public static void main(String[] args) { 36 | // Using Scanner 37 | Scanner objScanner = new Scanner(System.in); 38 | 39 | System.err.println("Strong Number Checker"); 40 | int number = 0; 41 | boolean validInput= false; 42 | 43 | //get input from user - loop until input is valid 44 | while(!validInput){ 45 | try { 46 | System.out.print("Enter a number: "); 47 | number = objScanner.nextInt(); 48 | validInput = true; 49 | } catch (InputMismatchException e) { 50 | System.out.println("Error. Please enter a valid integer."); 51 | objScanner.nextLine(); 52 | } 53 | } 54 | 55 | boolean example = isStrongNumber(number); 56 | 57 | if(example){ 58 | System.out.println("This number is a Strong Number!"); 59 | } else { 60 | System.out.println("This number is not a Strong Number."); 61 | } 62 | 63 | objScanner.close(); 64 | } 65 | } -------------------------------------------------------------------------------- /MP2/MP2-G10/Item2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item2 { 4 | public static boolean isHarshad(int intN) { 5 | int intSum = 0, intTemp = intN; 6 | while (intTemp > 0) { 7 | intSum += intTemp % 10; 8 | intTemp /= 10; 9 | } 10 | return intN % intSum == 0; 11 | } 12 | 13 | public static void harshadChecker(Scanner scanner) { 14 | while (true) { 15 | System.out.print("Enter any positive number: "); 16 | 17 | while (!scanner.hasNextInt()) { 18 | System.out.print("Invalid input! Please enter a positive integer: "); 19 | scanner.next(); 20 | } 21 | 22 | int intNum = scanner.nextInt(); 23 | 24 | while (intNum <= 0) { 25 | System.out.print("Not valid! Please input a positive integer only: "); 26 | while (!scanner.hasNextInt()) { 27 | System.out.print("Invalid input! Please enter a positive integer: "); 28 | scanner.next(); 29 | } 30 | intNum = scanner.nextInt(); 31 | } 32 | 33 | if (isHarshad(intNum)) 34 | System.out.println(intNum + " is a Harshad number."); 35 | else 36 | System.out.println(intNum + " is not a Harshad number."); 37 | 38 | System.out.print("\n"); 39 | 40 | char charChoice; 41 | while (true) { 42 | System.out.print("Do you want to check another number? (Y/N): "); 43 | String input = scanner.next().trim().toUpperCase(); 44 | 45 | if (input.length() == 1) { 46 | charChoice = input.charAt(0); 47 | 48 | if (charChoice == 'Y') { 49 | break; 50 | } 51 | else if (charChoice == 'N') { 52 | System.out.println("Exiting program. Goodbye!"); 53 | return; 54 | } 55 | } 56 | System.out.println("Invalid input! Please enter only Y or N."); 57 | } 58 | } 59 | } 60 | 61 | public static void main(String[] args) { 62 | System.out.println("This program determines whether the entered number is a Harshad number."); 63 | Scanner scanner = new Scanner(System.in); 64 | 65 | harshadChecker(scanner); 66 | scanner.close(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /MP2/group007-MP2/Item7.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item7 { 4 | static boolean isPrime(int number) { // Method to check if a number is a prime 5 | if (number < 2) 6 | return false; 7 | for (int i = 2; i <= Math.sqrt(number); i++) { // Checks divisors up to the sqrt only for efficiency 8 | if (number % i == 0) 9 | return false; 10 | } 11 | return true; 12 | } 13 | public static void main(String[] args) { 14 | Scanner scanner = new Scanner(System.in); 15 | int intFirstNumber; 16 | int intSecondNumber; 17 | System.out.print("Enter first number: "); 18 | if (!scanner.hasNextInt()) { // Error handling for non-integer input 19 | System.out.println("Invalid input. Please enter an integer."); 20 | return; 21 | } 22 | intFirstNumber = scanner.nextInt(); 23 | System.out.print("Enter second number: "); 24 | if (!scanner.hasNextInt()) { 25 | System.out.println("Invalid input. Please enter an integer."); 26 | return; 27 | } 28 | intSecondNumber = scanner.nextInt(); 29 | 30 | if (intFirstNumber < 2 || intSecondNumber < 2) { // Error handling for numbers < 2, since primes start at 2 31 | System.out.println("Numbers must be greater than or equal to 2."); 32 | return; 33 | } else if (intFirstNumber >= intSecondNumber) { // Error handling to make sure range is valid 34 | System.out.println("First number must be less than second number."); 35 | return; 36 | } 37 | 38 | System.out.println("The twin primes in this range are:"); 39 | boolean isFound = false; // Checks if any twin primes were found 40 | 41 | for (int i = intFirstNumber; i <= intSecondNumber - 2; i++) { // Loops through the range 42 | if (isPrime(i) && isPrime(i + 2)) { 43 | System.out.println(i + ", " + (i + 2)); 44 | isFound = true; 45 | } 46 | } 47 | if (!isFound) { // No twin primes found 48 | System.out.println("No twin primes found in this range."); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /MP2/group007-MP2/Item14.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Item14 { 4 | public static void main (String[] args) { 5 | Scanner scan = new Scanner(System.in); 6 | 7 | System.out.print("enter size of rows: "); 8 | int Row = scan.nextInt(); 9 | if (Row <= 0) { 10 | System.out.println("The size of rows must be greater than 0."); 11 | System.exit(0); 12 | } 13 | System.out.print("enter size of columns: "); 14 | int Column = scan.nextInt(); 15 | if (Column <= 0) { 16 | System.out.println("The size of columns must be greater than 0."); 17 | System.exit(0); 18 | } 19 | 20 | int[][] Matrix = TakeInput(Row, Column, scan); 21 | 22 | System.out.println("Original Matrix: "); 23 | Prints(Matrix, Row, Column); 24 | 25 | int[][] Transposed_Matrix = Transposing_Matrix(Matrix, Row, Column); 26 | 27 | System.out.println("\n\nTransposed Matrix: "); 28 | Prints(Transposed_Matrix, Column, Row); 29 | 30 | scan.close(); 31 | 32 | } 33 | 34 | static int[][] TakeInput(int Row, int Column, Scanner scan) { 35 | int[][] Returns_Matrix = new int[Row][Column]; 36 | for (int i = 0; i < Row; i++) { 37 | for (int j = 0; j < Column; j++) { 38 | System.out.printf("Enter a number(%d, %d): ", i, j); 39 | Returns_Matrix[i][j] = scan.nextInt(); 40 | } 41 | } 42 | return Returns_Matrix; 43 | } 44 | 45 | static void Prints(int[][] Matrix_To_Print, int Row, int Column) { 46 | for (int i = 0; i < Row; i++) { 47 | for (int j = 0; j < Column; j++) { 48 | System.out.print(Matrix_To_Print[i][j] + " "); 49 | } 50 | System.out.println(); 51 | } 52 | } 53 | 54 | static int[][] Transposing_Matrix (int[][] Matrix, int Row, int Column) { 55 | int[][] Transposed_Matrix = new int[Column][Row]; 56 | for (int i = 0; i < Row; i++) { 57 | for (int j = 0; j < Column; j++) { 58 | Transposed_Matrix[j][i] = Matrix[i][j]; 59 | } 60 | } 61 | return Transposed_Matrix; 62 | } 63 | 64 | } -------------------------------------------------------------------------------- /MP2/MP2-G3/Item15.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Item15 { 4 | 5 | public static void main(String[] args) { 6 | try (Scanner scanner = new Scanner(System.in)) { 7 | 8 | int n = 0; 9 | // Validate number of elements 10 | while (true) { 11 | System.out.print("Enter the number of elements: "); 12 | if (scanner.hasNextInt()) { 13 | n = scanner.nextInt(); 14 | if (n > 0) break; 15 | else System.out.println("Please enter a positive number."); 16 | } else { 17 | System.out.println("Invalid input! Please enter an integer."); 18 | scanner.next(); // clear invalid input 19 | } 20 | } 21 | 22 | int[] arr = new int[n]; 23 | 24 | System.out.println("Enter " + n + " numbers:"); 25 | for (int i = 0; i < n; i++) { 26 | while (true) { 27 | System.out.print("Element " + (i + 1) + ": "); 28 | if (scanner.hasNextInt()) { 29 | arr[i] = scanner.nextInt(); 30 | break; 31 | } else { 32 | System.out.println("Invalid input! Please enter an integer."); 33 | scanner.next(); // clear invalid input 34 | } 35 | } 36 | } 37 | 38 | boolean[] counted = new boolean[n]; 39 | 40 | System.out.println("\nFrequency of each element:"); 41 | for (int i = 0; i < n; i++) { 42 | if (counted[i]) continue; 43 | 44 | int count = 1; 45 | for (int j = i + 1; j < n; j++) { 46 | if (arr[i] == arr[j]) { 47 | count++; 48 | counted[j] = true; 49 | } 50 | } 51 | System.out.println(arr[i] + " appears " + count + " time(s)"); 52 | } 53 | 54 | } catch (Exception e) { 55 | System.out.println("An unexpected error occurred: " + e.getMessage()); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /MP2/MP2-G3/Item10.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | public class Item10 5 | { 6 | static Scanner console = new Scanner(System.in); 7 | public static void main (String[] args) 8 | { 9 | String wordOne = ""; 10 | String wordTwo = ""; 11 | 12 | System.out.println("Input word 1"); 13 | 14 | // Prevent empty input for word 1 15 | while (true) { 16 | wordOne = console.nextLine().trim(); 17 | if (!wordOne.isEmpty()) { 18 | break; 19 | } 20 | System.out.println("Invalid input! Please enter a non-empty word."); 21 | System.out.println("Input word 1"); 22 | } 23 | 24 | System.out.println("Input word 2"); 25 | 26 | // Prevent empty input for word 2 27 | while (true) { 28 | wordTwo = console.nextLine().trim(); 29 | if (!wordTwo.isEmpty()) { 30 | break; 31 | } 32 | System.out.println("Invalid input! Please enter a non-empty word."); 33 | System.out.println("Input word 2"); 34 | } 35 | 36 | System.out.println("Your Word 1: " + wordOne); 37 | System.out.println("Your Word 2: " + wordTwo); 38 | 39 | int wordOneLength = wordOne.length(); 40 | int wordTwoLength = wordTwo.length(); 41 | 42 | if(wordOneLength != wordTwoLength) 43 | { 44 | System.out.println("The words: " + wordOne + " and " + wordTwo + " are not Anagrams"); 45 | return; 46 | } 47 | 48 | String wordOneLow = wordOne.toLowerCase(); 49 | String wordTwoLow = wordTwo.toLowerCase(); 50 | char[] wordOneArray = wordOneLow.toCharArray(); 51 | char[] wordTwoArray = wordTwoLow.toCharArray(); 52 | Arrays.sort(wordTwoArray); 53 | Arrays.sort(wordOneArray); 54 | 55 | if(Arrays.equals(wordOneArray, wordTwoArray)) 56 | { 57 | System.out.println("The words: " + wordOne + " and " + wordTwo + " are Anagrams"); 58 | } 59 | else 60 | { 61 | System.out.println("The words: " + wordOne + " and " + wordTwo + " are NOT Anagrams"); 62 | } 63 | 64 | return; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Lecture 2 Codes Array String and Methods/MethodsDemo.java: -------------------------------------------------------------------------------- 1 | public class MethodsDemo { 2 | 3 | // ------------------ Simple Methods ------------------ 4 | void greet() { 5 | System.out.println("Hello! This is a simple method."); 6 | } 7 | 8 | void greetWithName(String strName) { 9 | System.out.println("Hello, " + strName + "!"); 10 | } 11 | 12 | // ------------------ Return Values ------------------ 13 | int square(int intNum) { 14 | return intNum * intNum; 15 | } 16 | 17 | // ------------------ Method Overloading ------------------ 18 | int add(int intNum1, int intNum2) { 19 | return intNum1 + intNum2; 20 | } 21 | 22 | double add(double dblNumber1, double dblNumber2) { 23 | return dblNumber1 + dblNumber2; 24 | } 25 | 26 | // ------------------ Static Methods ------------------ 27 | static void staticMessage() { 28 | System.out.println("This is a static method."); 29 | } 30 | 31 | // ------------------ Recursion ------------------ 32 | int factorial(int intNum) { 33 | if (intNum == 0) return 1; 34 | return intNum * factorial(intNum - 1); 35 | } 36 | 37 | // ------------------ Method Overriding ------------------ 38 | static class Animal { 39 | void sound() { 40 | System.out.println("The animal makes a sound."); 41 | } 42 | } 43 | 44 | static class Dog extends Animal { 45 | @Override 46 | void sound() { 47 | System.out.println("The dog barks."); 48 | } 49 | } 50 | 51 | // ------------------ Main Method ------------------ 52 | public static void main(String[] args) { 53 | MethodsDemo demo = new MethodsDemo(); 54 | 55 | // Simple Methods 56 | demo.greet(); 57 | demo.greetWithName("Chris"); 58 | 59 | // Return Values 60 | int intResult = demo.square(5); 61 | System.out.println("Square of 5 is: " + intResult); 62 | 63 | // Overloading 64 | System.out.println("Sum of 5 + 10 = " + demo.add(5, 10)); 65 | System.out.println("Sum of 2.5 + 3.7 = " + demo.add(2.5, 3.7)); 66 | 67 | // Static Method 68 | MethodsDemo.staticMessage(); 69 | 70 | // Recursion 71 | System.out.println("Factorial of 5 = " + demo.factorial(5)); 72 | 73 | // Overriding 74 | Animal objAnimal = new Animal(); 75 | objAnimal.sound(); 76 | 77 | Dog objDog = new Dog(); 78 | objDog.sound(); 79 | } 80 | } 81 | --------------------------------------------------------------------------------