---
# System prepended metadata

title: 50.001 PSET 1A

---

# 50.001 PSET 1A

## Week 1

### Cohort Sessions

- [x] Fibonacci (Session 1)

<details> 
    <summary>Answer</summary>
    
```java=
public class Fibonacci{
    public static String fibonacci( int n ) {
        int arr[] = new int[n];
        String answer_string="0";
        for(int i =0;i<n;i++){
            if (i == 0 || i == 1) arr[i] = i;
            else arr[i] = arr[i-2]+arr[i-1];
        }
        for(int i=1;i<n;i++){
            answer_string += (","+arr[i]);
        }
        return answer_string;
    }
}
```
</details>
<br/>

- [x] Iterating with Iterator (Session 2)
<details>
    <summary>Answer</summary>
    
```java=
public class IteratingExamples {

    public static int Act2Iterator(List<Integer> integers) {
        int sum = 0;
        Iterator<Integer> iter = integers.iterator();
        while(iter.hasNext()){
            sum += iter.next();
        }
        return sum;
    }
}
```
</details>
<br/>

- [x] Iterating with For-Each (Session 2)
<details>
    <summary>Answer</summary>
    
```java=
public class IteratingExamples {

      public static int Act2ForEach(List<Integer> integers) {
          int sum = 0;
          for(Integer i:integers){
              sum+=i;
          }
          return sum;
      }
}

```
</details>
<br/>

- [x] Bonus: Hailstone sequence

<details>
    <summary>Answer</summary>

```java=
public static int[] getHS(int n){
         int[]arr = new int[n];
         int counter = 0;
         while(n>=1){
             if(n==1){
                 arr[counter]=n;
                 break;
             }
             arr[counter]=n;
             if(n%2==0)n/=2;
             else n = n*3+1;
             counter++;
         }
         return arr;
}

```
</details>
<br/>


- [x] The Account Class (Session 3)

<details>
    <summary> Answer </summary>
    
```java=
    public class Account {
    int id = 0;
    double balance = 0.0;
    static double annualInterestRate = 0.0;
    Date dateCreated = new Date();

    public Account() {
    } // no arg constructor

    public Account(int id, double balance) {
        this.id = id;
        this.balance = balance;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public static double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public static void setAnnualInterestRate(double newRate) {
        annualInterestRate = newRate;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public void withdraw(double amount) {
        this.balance -= amount;
    }

    public void deposit(double amount) {
        this.balance += amount;
    }

    public static double getMonthlyInterestRate() {
        return annualInterestRate / 12;
    }

    public double getMonthlyInterest() {
        return getMonthlyInterestRate()/100 * getBalance();
    }
}
```
</details>
<br/>

### Homework Questions
- [x] Prime Number Checker
<details>
    <summary>Answer</summary>
    
```java=
public class PrimeNumberChecker{
    public static int isPrime(int num){
        for(int i=2;i<num;i++) if (num%i==0) return 0;
        return 1;
    }
}
```
</details>
<br/>

- [x] Geometry: The MyRectangle2D class
- [x] String Operations

<details>
    <summary>Answer</summary>
    
```java=
import java.util.Arrays;
import java.util.Set;

public class Pset1 {	
	public static boolean isAllCharacterUnique(String sIn) {
        char[] arr = sIn.toCharArray();
        HashSet<Character> unique = new HashSet<Character>();
        for (char element : arr)
            unique.add(element);
        if (unique.size() == arr.length){
            return true;
        }
        else{
            return false;
        }
    }
	public static boolean isPermutation(String sIn1, String sIn2) {
	  char[] arr1 = sIn1.toCharArray();
      char[] arr2 = sIn2.toCharArray();
      Arrays.sort(arr1);
      Arrays.sort(arr2);
      if(arr1.length == arr2.length){
          for(int i=0;i<arr1.length;i++)if(arr1[i]!=arr2[i]) return false;
          return true;
      }
      else return false;
    }
}
```
</details>
}