Tuesday, February 12, 2019

Java Class

Java Class
- An object is an instance of a class.
- Non-primitive variables are references to objects.
- Objects can have multiple references.

Creation of Instances 
- A class can have multiple objects created/constructed.
- A class has only one blueprint.
- An object can only be created using the key word "new".
- When "new" is called, the object is instantiated calling the "constructor method".
- String values are instances of java.lang.String
      String str = new String("string");
      String str = "string";
Static vs Dynamic * Instance variables vs Class variables - Instance variable is a member of an instance/objects, not a member of the class * Instance methods vs Class methods
Java Strings are immutable, meaning?
That means that once you instantiate and assign their values, you can't really change them.
Java compiler will make it look like you can change them.
      String str = "Hello, world!";
      str = "new string";
it looks like you are changing the object. But in fact, in the background, the Java compiler is de-referencing the original object. That object can now be cleared from memory in a process known as garbage collection, and a brand new object is created with the new value, which is now pointed to by "str". That is, Java reset the value of a String by creating a new object and give that to you.
Examples
public class Bicycle {
    // the Bicycle class has
    // three fields
    public int cadence;
    public int gear;
    public int speed;
        
    // the Bicycle class has
    // one constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }
        
    // the Bicycle class has
    // four methods
    public void setCadence(int newValue) {
        cadence = newValue;
    }
        
    public void setGear(int newValue) {
        gear = newValue;
    }
        
    public void applyBrake(int decrement) {
        speed -= decrement;
    }
        
    public void speedUp(int increment) {
        speed += increment;
    }
}
public class MountainBike extends Bicycle {
 // the MountainBike subclass has
    // one field
    public int seatHeight;

    // the MountainBike subclass has
    // one constructor
    public MountainBike(int startHeight, int startCadence,
                        int startSpeed, int startGear) {
        super(startCadence, startSpeed, startGear);
        seatHeight = startHeight;
    }   
        
    // the MountainBike subclass has
    // one method
    public void setHeight(int newValue) {
        seatHeight = newValue;
    }
}
import java.io.*;
import java.util.*;

class gift1 {

    public static void main(String[] args) throws IOException {
        BufferedReader f = new BufferedReader(new FileReader("gift1.in"));
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("gift1.out")));
        StringTokenizer st = new StringTokenizer(f.readLine());

        int n = Integer.parseInt(st.nextToken());
        Map<String, List<String>> giveto = new HashMap giftmoney = new HashMap<String, Gift>();

        String[] groups = new String[n];
        for (int i = 0; i < n; i++) {
            groups[i] = f.readLine();
            System.out.println(groups[i]);
        }
        for (int i = 0; i < n; i++) {
            String s = f.readLine();
            List<String> friends = new ArrayList<String>();

            st = new StringTokenizer(f.readLine());
            int totalgiftmoney = Integer.parseInt(st.nextToken());
            int givetoNum = Integer.parseInt(st.nextToken());
            for (int j = 0; j < givetoNum; j++ ) {
                String friend = f.readLine();
                friends.add( friend );
            }
            Gift g = new Gift(totalgiftmoney, givetoNum);
            giveto.put(s, friends);
            giftmoney.put(s, g);
            for ( Map.Entry<String,Gift> m : giftmoney.entrySet() ) {
                System.out.println(m.getKey());
                System.out.println(m.getValue().total);
            }
        }
        f.close();
        out.close();
    }

}
class Gift
{
    public int total = 0;
    public int toNum = 0;
    public void Gift () {
    }

    public Gift (int total, int num) {
        this.total = total;
        this.toNum = num;
    }
}
Java Class Operators
// instanceof // Class membership
String s = "string";
System.out.println( s instanceof java.lang.String );

// Strings cannot be compared using ==
// Strings should use the String.equals to compare

if (s1.equals(s2)) { System.out.println ("equal"); }
else               { System.out.println ("different"); }

No comments:

Post a Comment