Skip to content

lab completed #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/lab-java-standard-input-and-classes.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
185 changes: 185 additions & 0 deletions out/production/lab-java-standard-input-and-classes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png)

# LAB Java | Standard Input and Classes

## Introduction

We have just learned how to use input/output in the console and files, so let's practice a bit more.

<br>

## Requirements

1. Fork this repo.
2. Clone this repo.
3. Add your instructor and the class graders as collaborators to your repository. If you are unsure who your class graders are, ask your instructor or refer to the day 1 slide deck.
4. In the repository, create a Java project and add the code for the following prompts.

## Submission

Once you finish the assignment, submit a URL link to your repository or your pull request in the field below.

<br>

## Instructions

### Employee and Intern Classes

1. Create an `Employee` class to represent an employee of a company. The class should have `name`, `email`, `age`, `salary` properties and appropriate getters and setters.
2. Create an `Intern` class that extends from `Employee`. The `Intern` class should have a salary limit of 20000 (constant).
3. Implement validation in the `Intern` class to ensure that an intern is not created (or salary updated) with a salary that exceeds the maximum allowed value.
4. Write a program that creates 10 `Employee` objects and prints all of their properties (name, email, age, salary) to a file named `employees.txt`.

<br>

### Tips

- Be sure to consider the relationship between the `Employee` and `Intern` classes when designing your solution.
- Think about the different properties and behaviors that are applicable to both `Employee` and `Intern` objects and consider how you can use inheritance to avoid duplication of code.
- Use appropriate access modifiers (e.g. `private`, `protected`, `public`) to control the visibility of your class properties and methods.
- Make sure to handle edge cases and handle them appropriately (e.g. what happens if an `Intern` object is created with a salary that exceeds the maximum allowed value?).

<br>

## FAQs

<br>

<details>
<summary style="font-size: 16px; cursor: pointer; outline: none; font-weight: bold;">I am stuck and don't know how to solve the problem or where to start. What should I do?</summary>

<br> <!-- ✅ -->

If you are stuck in your code and don't know how to solve the problem or where to start, you should take a step back and try to form a clear, straight forward question about the specific issue you are facing. The process you will go through while trying to define this question, will help you narrow down the problem and come up with potential solutions.

For example, are you facing a problem because you don't understand the concept or are you receiving an error message that you don't know how to fix? It is usually helpful to try to state the problem as clearly as possible, including any error messages you are receiving. This can help you communicate the issue to others and potentially get help from classmates or online resources.

Once you have a clear understanding of the problem, you should be able to start working toward the solution.

</details>

<br>

<details>
<summary style="font-size: 16px; cursor: pointer; outline: none; font-weight: bold;">How do I create a Maven project in IntelliJ?</summary>

<br> <!-- ✅ -->

To create a Maven project in IntelliJ, you can follow these steps:

1. Open IntelliJ IDEA and click the "Create New Project" button.
2. In the "New Project" dialog, select "Maven" as the build system.
3. Specify the name of the project.
4. In the "Project Location" section, specify a location where you want to save your project.
5. Select the "Create Git repository" checkbox in order to initialize the git repository upon creation of the project.
6. Click the "Create" button to create the Maven project.


</details>

<br>

<details>
<summary style="font-size: 16px; cursor: pointer; outline: none; font-weight: bold;">What is inheritance in Java and how do I extend a class to another class?</summary>

<br> <!-- ✅ -->

Inheritance in Java is a mechanism that allows you to create a new class (the child class) based on an existing class (the parent class). The child class inherits the properties and methods of the parent class, which means that you can reuse the code and add new functionality to the child class.

To extend a class to another class, you use the `extends` keyword in the child class declaration. The parent class is specified after the `extends` keyword. For example:

```java
class ParentClass {
int x;

void display() {
System.out.println("This is the ParentClass");
}
}

class ChildClass extends ParentClass {
int y;

void display() {
System.out.println("This is the ChildClass");
}
}
```

In this example, `ChildClass` extends `ParentClass`, so it inherits the `x` variable and the `display()` method. You can also add new properties and methods to the child class, such as the y variable and the `display()` method override in the child class.

It's important to note that the child class can access all the properties and methods of the parent class that have public or protected visibility. Properties and methods with private visibility are not inherited by the child class.

</details>

<br>

<details>
<summary style="font-size: 16px; cursor: pointer; outline: none; font-weight: bold;">What is the difference between using "PrintWriter" and "FileWriter" in Java and how can they be used together?</summary>

<br> <!-- ✅ -->

`PrintWriter` and `FileWriter` are two classes in Java that are used to write data to a file. However, they serve different purposes and have different features.

`FileWriter` is a basic class in Java that provides simple file output functions. You can use it to write text to a file, but it does not provide any formatting options, such as controlling the number of characters per line or specifying the line separator.

`PrintWriter`, on the other hand, provides advanced printing functions, including formatting options and automatic line flushing. It also provides a more user-friendly API for writing to a file compared to `FileWriter`.

Here's how you can use `FileWriter` and `PrintWriter` together in a program:

```java
import java.io.*;

public class WriteToFile {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("output.txt");
PrintWriter pw = new PrintWriter(fw);
pw.println("This is a line written using PrintWriter");
pw.println("Another line written using PrintWriter");
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```

In this example, `FileWriter` is used to open a file named `output.txt` and `PrintWriter` is used to write lines of text to the file. The `println` method in `PrintWriter` automatically adds a line separator after each line of text, making it easier to write formatted text to a file.

It's important to note that you need to close both `FileWriter` and `PrintWriter` when you are done writing to the file, as shown in the code above.

</details>

<br>

<details>
<summary style="font-size: 16px; cursor: pointer; outline: none; font-weight: bold;">I am unable to push changes to my repository. What should I do?</summary>

<br> <!-- ✅ -->

If you are unable to push changes to your repository, here are a few steps that you can follow:

1. Check your internet connection: Ensure that your internet connection is stable and working.
1. Verify your repository URL: Make sure that you are using the correct repository URL to push your changes.
2. Check Git credentials: Ensure that your Git credentials are up-to-date and correct. You can check your credentials using the following command:

```bash
git config --list
```

4. Update your local repository: Before pushing changes, make sure that your local repository is up-to-date with the remote repository. You can update your local repository using the following command:

```bash
git fetch origin
```

5. Check for conflicts: If there are any conflicts between your local repository and the remote repository, resolve them before pushing changes.
6. Push changes: Once you have resolved any conflicts and updated your local repository, you can try pushing changes again using the following command:

```bash
git push origin <branch_name>
```

</details>
11 changes: 11 additions & 0 deletions out/production/lab-java-standard-input-and-classes/employees.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
The currents employees & interns in our company are:
Name: Carlos Email: [email protected] Age: 20 Salary: 19999.0
Name: Clarymar Email: [email protected] Age: 19 Salary: 20000.0
Name: Ortiz Email: [email protected] Age: 19 Salary: 500.0
Name: Luisito Email: [email protected] Age: 19 Salary: 19999.0
Name: Isoelis Email: [email protected] Age: 25 Salary: 100001.0
Name: Marie Email: [email protected] Age: 25 Salary: 100000.0
Name: Iris Email: [email protected] Age: 42 Salary: 90000.0
Name: Hugo Email: [email protected] Age: 45 Salary: 45000.0
Name: Javier Email: [email protected] Age: 46 Salary: 44000.0
Name: Pito Email: [email protected] Age: 48 Salary: 40000.0
50 changes: 50 additions & 0 deletions src/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package src;//Author: Carlos J. Pepin Delgado<[email protected]>

public class Employee {

private String name;
private String email;
private int age;
private double salary;

public Employee(String name, String email, int age, double salary) {
this.setName(name);
this.setEmail(email);
this.setAge(age);
this.setSalary(salary);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}


}
23 changes: 23 additions & 0 deletions src/Intern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package src;//Author: Carlos J. Pepin Delgado<[email protected]>

public class Intern extends Employee{

public final double SALARY_LIMIT = 20000; //SALARY LIMIT FOR INTERNS
private double salary;

public Intern(String name, String email, int age, double salary) {
super(name, email, age, salary);
if(salary>SALARY_LIMIT){
throw new IllegalArgumentException("Salary for intern cannot exceed "+SALARY_LIMIT);
}

}

@Override
public void setSalary(double salary){
if(salary>SALARY_LIMIT){
throw new IllegalArgumentException("Salary for intern cannot exceed "+SALARY_LIMIT);
}
super.setSalary(salary);
}
}
35 changes: 35 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package src;//Author: Carlos J. Pepin Delgado<[email protected]>

import java.io.*;

public class Main {

public static void main(String[] args) throws IOException {
//EMPLOYEE CREATION
Employee e1 =new Intern("Carlos","[email protected]",20,19999);
Employee e2 =new Intern("Clarymar","[email protected]",19,20000);
Employee e3 =new Intern("Ortiz","[email protected]",19,500);
Employee e4 =new Intern("Luisito","[email protected]",19,19999);
Employee e5 =new Employee("Isoelis","[email protected]",25,100001);
Employee e6 =new Employee("Marie","[email protected]",25,100000);
Employee e7 =new Employee("Iris","[email protected]",42,90000);
Employee e8 =new Employee("Hugo","[email protected]",45,45000);
Employee e9 =new Employee("Javier","[email protected]",46,44000);
Employee e10 =new Employee("Pito","[email protected]",48,40000);

Employee[] staff={e1,e2,e3,e4,e5,e6,e7,e8,e9,e10};

//DOCUMENT GENERATOR & MODIFIER
FileWriter fw = new FileWriter("src/employees.txt");
fw.write("The currents employees & interns in our company are: \n");

for (Employee e : staff) {
fw.write("Name: " + e.getName() + " ");
fw.write("Email: " + e.getEmail() + " ");
fw.write("Age: " + e.getAge() + " ");
fw.write("Salary: " + e.getSalary() + "\n");
}

fw.close();
}
}
Loading