Due Date: March 26, 2024
PX_AdvSubstring_lastname(4 files)
Purpose: To build a java programs uses string commands.
/* Instructions:
* Copy the java program code below into Eclipse.
* I injected to 3 errors in the code.
* Find the syntax errors and correct them.
* The logic is solid. I only created syntax errors.
*/
You will need to create the following 4 files:
PX_AdvSubstring_lastname.java (Actual Java program)
PX_AdvSubstring_lastname.txt (Copy of Java program)
PX_AdvSubstring_lastname.png (Screen shot of program running)
PX_AdvSubstring_lastname.mp4 (Video of you running the program)
(You will need to verbally describe the program in your video)
Due Date: March 26, 2024
import java.util.Scanner;
public class AdvancedSubstringSearch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input a string
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
// Input substrings to search for
System.out.print("Enter substrings to search for (comma-separated): ");
String[] substrings = scanner.nextLin().split(",");
// Ask the user if the search should be case-sensitive
System.out.printIn("Do you want the search to be case-sensitive? (yes/no): ");
boolean caseSensitive = scanner.nextLine().equalsIgnoreCase("yes");
System.out.println("\nResults:");
for (String substring : substrings) {
System.out.println("Searching for substring: " + substring);
int index = caseSensitive ? inputString.indexOf(substring) : inputString.toLowerCase().indexOf(substring.toLowerCase());
int occurrences = 0;
while (index != -1) {
occurrences++;
System.out.println("Found at index " + index);
if (index + 1 >= inputString.length()) {
break;
}
index = caseSensitive ? inputString.indexOf(substring, index + 1) : inputString.toLowerCas().indexOf(substring.toLowerCase(), index + 1);
}
if (occurrences == 0) {
System.out.println("Substring '" + substring + "' not found in the input string.");
} else {
System.out.println("Total occurrences of '" + substring + "': " + occurrences);
}
System.out.println();
}
scanner.close();
}
}
Due Date: April 5, 2024
Assignment Learn about java inheritence
Java Inheritance Explanation
Please read all of this.
Basic Concepts
- **Superclass (Parent Class):** The class whose features are inherited is known as the superclass
or parent class.
- **Subclass (Child Class):** The class that inherits the features from another class is known as the
subclass or child class. A subclass can add its own fields and methods in addition to the superclass
fields and methods.
- **`extends` Keyword:** In Java, inheritance is achieved using the `extends` keyword. A class that
inherits from another class uses `extends` to specify the superclass from which it is inheriting.
- **Method Overriding:** Subclasses can provide specific implementations for methods they
inherited from the superclass. This is known as overriding.
- **`super` Keyword:** The `super` keyword is a reference variable that is used to refer parent class
objects. It can be used to invoke superclass methods and to access the superclass constructor.
- **Inheritance Hierarchy:** Java supports single inheritance, meaning that a class can only extend
one other class, leading to a single inheritance hierarchy. However, a class can implement multiple
interfaces, allowing for a form of multiple inheritance.
Example of Inheritance in Java
Let's illustrate inheritance with a simple example. We'll create a superclass named `Animal` and a
subclass named `Dog` that inherits from `Animal`.
Your file names will be:
PX_Animals_lastname.java (Java program)
PX_Animals_lastname.png (Screen print of your program in the Eclipse IDE)
PX_Dog_lastname.java (Java program)
PX_Dog_lastname.png (Screen print of your program in the Eclipse IDE)
PX_AnimalDriver_lastname.java (Java program)
PX_AnimalDriver_lastname.png (Screen print of your program in the Eclipse IDE)
PX_AnimalDriver_lastname.mp4 (Video of AnimalDriver)
PX_AnimalDriver_Board_lastname.png (Explain on the Board.)
Be sure to drop off your files into google classroom.
Code is below.
Be sure to indent your code.
This will 3 different classes.
#### Superclass: Animal
```java
public class Animal
{
Java Inheritance Explanation
String type = "Animal";
public void eat() {
System.out.println("This animal eats food.");
}
}
```
#### Subclass: Dog
```java
public class Dog extends Animal { // Dog inherits from Animal
public void displayType() {
System.out.println("The type is: " + type);
}
@Override
public void eat() {
System.out.println("This dog eats dog food.");
}
}
```
#### Main Class
Java Inheritance Explanation
```java
public class AnimalDriver {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.displayType(); // Displays: The type is: Animal
myDog.eat(); // Displays: This dog eats dog food.
}
}
```
Key Points in the Example
- **Inheritance:** The `Dog` class inherits from the `Animal` class using the `extends` keyword.
- **Access to Superclass Fields:** The `Dog` class can access the `type` field from `Animal`.
- **Method Overriding:** The `Dog` class provides its own implementation of the `eat()` method,
overriding the implementation provided by `Animal`.
- **Code Reusability:** The `Dog` class reuses the fields and methods of the `Animal` class,
demonstrating the code reusability aspect of inheritance.
Inheritance is a powerful feature of Java and object-oriented programming that helps in organizing
and structuring code in a clear, hierarchical manner.