1.Assign
public class Assign {
public static void main (String args []) {
// declare integer variables
int x, y;
// declare and assign floating point
float z = 3.414f;
// declare and assign double
double w = 3.1415;
// declare and assign boolean
boolean truth = true;
// declare character variable
char c;
// declare String variable
String str;
// declare and assign String variable
String str1 = "bye";
// assign value to char variable
c = 'A';
// assign value to String variable
str = "Hi out there!";
// assign values to int variables
x = 6;
y = 1000;
}
}
public class Assign {
public static void main (String args []) {
// declare integer variables
int x, y;
// declare and assign floating point
float z = 3.414f;
// declare and assign double
double w = 3.1415;
// declare and assign boolean
boolean truth = true;
// declare character variable
char c;
// declare String variable
String str;
// declare and assign String variable
String str1 = "bye";
// assign value to char variable
c = 'A';
// assign value to String variable
str = "Hi out there!";
// assign values to int variables
x = 6;
y = 1000;
}
}
2. MyDate
public class MyDate {
private int day;
private int month;
private int year;
public MyDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public MyDate(MyDate date) {
this.day = date.day;
this.month = date.month;
this.year = date.year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public MyDate addDays(int more_days) {
MyDate new_date = new MyDate(this);
new_date.day = new_date.day + more_days;
// Not Yet Implemented: wrap around code...
return new_date;
}
public String toString() {
return "" + day + "-" + month + "-" + year;
}
}
3. PassTest
public class PassTest {
// Methods to change the current values
public static void changeInt(int value) {
value = 55;
}
public static void changeObjectRef(MyDate ref) {
ref = new MyDate(1, 1, 2000);
}
public static void changeObjectAttr(MyDate ref) {
ref.setDay(4);
}
public static void main(String args[]) {
MyDate date;
int val;
// Assign the int
val = 11;
// Try to change it
changeInt(val);
// What is the current value?
System.out.println("Int value is: " + val);
// Assign the date
date = new MyDate(22, 7, 1964);
// Try to change it
changeObjectRef(date);
// What is the current value?
System.out.println("MyDate: " + date);
// Now change the day attribute
// through the object reference
changeObjectAttr(date);
// What is the current value?
System.out.println("MyDate: " + date);
}
}
4. TestMyDate
public class TestMyDate {
public static void main(String[] args) {
MyDate my_birth = new MyDate(22, 7, 1964);
MyDate the_next_week = my_birth.addDays(7);
System.out.println(the_next_week);
}
}
No comments:
Post a Comment