15.3

public class Complex{
	public float real;
	public floar imag;

	public Complex(){ 
	  real = 0; 
	  imag = 0;
	}

	public Complex(float r, float i){
	  real = r;
	  imag = i;
	}

	public Complex add(Complex a, Complex b){
	   return new Complex(a.r + b.r, a.i + b.i);
	}	
}

15.6

public class ComplexTest{
	public static void main(String[] args) {
		Complex c1 = new Complex(1.0, 2.0);
		Complex c2 = new Complex(3.1, -3.4);
	}
}


public class Rectangle{

	public int s1, s2, s3, s4;

	public Rectangle(int s1, int s2, int s3, int s4){
		if(s1 == s2)
			if (s3 != s4) 
				System.exit(0);
		else if(s2 == s3)
			if (s1 != s4) 
				System.exit(0);
		else if(s1 == s3)
			if (s2 != s4) 
				System.exit(0);
		else {
			this.s1=s1;
			this.s2=s2;
			this.s3=s3;
			this.s4=s4;
		}
	}

	public Rectangle(int s1, int s2){
		return this(s1, s2, s1, s2);
	}
}

public class RectangleObjectCreate {

	public static void main(String[] args){
		Rectangle r1 = new Rectangle(1,2,3,4);
		Rectangle r2 = new Rectangle(1,2);
	}
}



16.5

At the point where the code says // before, do p and q refer to the same object? Why or why not?
>> No, because they are each set to different objects of type person.


At the point where the code says // after, do p and q refer to the same object? Why or why not?
>> Yes, because q was just set to point to the Person object referenced by p.


17.3

What does the following code output? Why?

The code outputs "null" because even though you are creating a new object of type person, the field name still isn't assigned a value.


18.3

import java.lang.Math;

public class Complex{
	public float real;
	public floar imag;

	public Complex(){ 
	  real = 0; 
	  imag = 0;
	}

	public Complex(float r, float i){
	  r = real;
	  i = imag;
	}

	public static Complex add(Complex a, Complex b){
	   return new Complex(a.r + b.r, a.i + b.i);
	}	

	public String toString() {
	   return (imag > 0) ? (real + " + i" + imag) : (real + " - i" + Math.abs(imag));
	}
}


19.3


In the above example, try to access and change ds y and z fields. Follow the same order of output statements:
 Why does d.y output as 20 before you change it?
>>>>>>Because that is a default value you set for y in the class declaration.

 Why does your program not allow you to access d.z?
>>>>>>Because z is a private member field of Data, it can only be accessed by methods inside the class Data.


-Rewrite Data such that it has no public fields, but you can still access and change its fields.

class Data {
	private int x;
	private int y=20;
	private int z;

	public int getx() { return x; }
	public int gety() { return y; }
	public int getz() { return z; }

	public void setx(int a) { x = a; }
	public void sety(int a) { y = a; }
	public void setz(int a) { z = a; }

}




-Expand class Person to include a last name, first name, and methods to access those names.


class Person {
	private int age;
	private String firstname;
	private String lastname;

	public Person(int a, String f, String l) { age = a; firstname = f; lastname = l}
	public int getAge() { return age; }

	public String getFirstname() { return firstname; }
	public String getLastname() { return lastname; }
	
	public void setFirstname(String f) { firstname = f; }
	public void setLastname(String l) { lastname = l; }
}



-Write a program that adds two complex numbers together. Report the output.


public class ComplexAdd{
	public static void main(String[] args){
		Complex c1 = new Complex(1.0, 2.0);
		Complex c2 = new Complex(3.1, -3.4);
		Complex sum = Complex.add(c1,c2);

		System.out.println(sum);
	}
}

The output should be: 4.1 - i1.4	
