




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































private static void exit() { System.out.println( "Usage: java CelsiusToFahrenheit
Implementation
Testing
Design
CombinationLock -n1:int -n2:int -n3:int -open:boolean +changeComb(int,int,int,int,int,int):boolean +close() +isOpen():boolean +open(int,int,int):boolean
Section
Person
Instructor Student
Course
Section
Person
Instructor Student
Course
Chair
Department
Section
Person
Instructor Graduate
Course
Student
Undergraduate
Person
Driver Owner
Vehicle
Bus Car
public interface Temperature { /** @return the Celsius value for this temperature. / public double getCelsius(); /* @return the Fahrenheit value for this temperature. / public double getFahrenheit(); /* @return the Kelvin value for this temperature. / public double getKelvin(); /* @param celsius the Celsius value for this temperature. / public void setCelsius(double celsius); /* @param fahrenheit the Fahrenheit value for this temp. / public void setFahrenheit(double fahrenheit); /* @param kelvin the Kelvin value for this temperature.*/ public void setKelvin(double kelvin); }
public class MyTemperature implements Temperature { private double celsius; // stores temperature as a Celsius value public MyTemperature(double value, char scale) { if (scale=='C') setCelsius(value); if (scale=='F') setFahrenheit(value); else setKelvin(value); } public double getCelsius() { return celsius; } public double getFahrenheit() { return 9celsius/5 + 32.0; } public double getKelvin() { return celsius + 273.16; } public void setCelsius(double celsius) { this.celsius = celsius; } public void setFahrenheit(double fahrenheit) { this.celsius = 5(fahrenheit - 32)/9; } public void setKelvin(double kelvin) { this.celsius = kelvin - 273.16; } public String toString() { // Example: "25.0 C = 77.0 F"
return round(getCelsius())+ " C = "
public double getFahrenheit() { return 9celsius/5 + 32.0; } public void setCelsius(double celsius) { this.celsius = celsius; } public void setDigits(int digits) { this.digits = digits; setFormatter(); } public void setFahrenheit(double fahrenheit) { this.celsius = 5(fahrenheit - 32)/9; } public String toString() { // Example: "25.0 C = 77.0 F" return formatter.format(getCelsius()) + " C = "
1.4 public class TestPrimeAlgorithm { public static void main(String[] args) { Random random = new Random(); for (int i=0; i<100; i++) { int n = random.nextInt(Integer.MAX_VALUE); if (isPrime(n)) System.out.print(n + " "); } } public static boolean isPrime(int n) { if (n < 2) return false; if (n < 4) return true; if (n%2 == 0) return false; for (int d=3; d*d <= n; d += 2) if (n%d == 0) return false; return true; } } 1.5 public class Course { private float credit; private String dept; private String id; private String name; private Section[] sections = new Section[1000]; public Course(String dept, String id, String name, float credit) { this.credit = credit; this.dept = dept; this.id = id; this.name = name; } public void add(Section section) { int i=0; while (sections[i] != null) ++i; sections[i] = section; } public String toString() { String s = dept + " " + id + " "" + name + "", "
public void setTime(String time) { this.time = time; } public void setInstructor(Instructor instructor) { this.instructor = instructor; } public void setStudents(Student[] students) { int n = students.length; // duplicate the array object: this.students = new Student[n]; // but do not duplicate the Student objects: for (int i=0; i<n; i++) this.students[i] = students[i]; } public String toString() { return course + ": " + term + ", " + place + ", " + time + ", "
import java.util.*; public class Person { protected int yob; protected String email; protected String id; protected boolean male; protected String name; public Person(String name, String id, String sex, int yob) { this.id = id; this.male = (sex.substring(0,1).toUpperCase() == "M"); this.name = name; this.yob = yob; } public int getYob() { return yob; }
public String getEmail() { return email; } public String getId() { return id; } public boolean isMale() { return male; } public String getName() { return name; } public void setEmail(String email) { this.email = email; } public String toString() { String string = name + ", " + id; if (male) string += " (M)"; else string += " (F)"; if (email != null) string += ", " + email; string += " (" + yob + ")"; return string; } public static void main(String[] args) { Person grandson = new Person("C. Hubbard", "1.2.1", "M", 2002); System.out.println(grandson); grandson.setEmail("[email protected]"); System.out.println(grandson); System.out.println("\t name: " + grandson.name); System.out.println("\t id: " + grandson.id); System.out.println("\t sex: "+ (grandson.male?"male":"female")); System.out.println("\temail: " + grandson.email); System.out.println("\t yob: " + grandson.yob); } }
import java.util.*; public class Student extends Person { protected String country; protected int credits; protected double gpa; public Student(String name, String id, String s, int y, String c) {
public String getTel() { return tel; } public void setDept(String dept) { this.dept = dept; } public void setOffice(String office) { this.office = office; } public void setTel(String tel) { this.tel = tel; } public String toString() { String s = super.toString(); if (dept != null) s += ", " + dept; if (office != null) s += ", " + office; if (tel != null) s += " (" + tel + ")"; return s; } public static void main(String[] args) { Instructor knuth = new Instructor("Don Knuth","8122063","M",1938); System.out.println(knuth); knuth.setDept("CS"); System.out.println(knuth); } } 1.6 public class ComboLock { private int n1, n2, n3; private boolean open; public ComboLock(int n1, int n2, int n3) { this.n1 = n1; this.n2 = n2; this.n3 = n3; } public boolean changeComb(int n1, int n2, int n3, int n4, int n5, int n6) { if (this.n1 != n1 || this.n2 != n2 || this.n3 != n3) return false; this.n1 = n4; this.n2 = n5; this.n3 = n6; open = false; return true;
public void close() { open = false; } public boolean isOpen() { return open; } public boolean open(int n1, int n2, int n3) { if (this.n1 == n1 && this.n2 == n2 && this.n3 == n3) open = true; else open = false; return open; } }
public class TestComboLock { public static void main(String[] args) { ComboLock lock = new ComboLock(10, 20, 30); System.out.println("lock.isOpen(): " + lock.isOpen()); System.out.println("lock.open(10,20,30): "+lock.open(10,20,30)); System.out.println("lock.isOpen(): " + lock.isOpen()); lock.close(); System.out.println("lock.isOpen(): " + lock.isOpen()); System.out.println("lock.open(11,20,30): "+lock.open(11,20,30)); System.out.println("lock.isOpen(): " + lock.isOpen()); System.out.println("lock.changeComb(11, 20, 30, 11, 22, 33): "
public String toString() { return "(" + areaCode + ")" + number.substring(0, 3)
public class Person { private final boolean male; private final String name; private final Phone phone; private final int yob; public Person(String name, boolean male, int yob, Phone phone) { this.name = name; this.male = male; this.yob = yob; this.phone = new Phone(phone); } public String getName() { return name; } public Phone getPhone() { return phone; } public int getYob() { return yob; } public boolean isMale() { return male; } public String toString() { return (male?"Mr. ":"Ms. ") + name+" ("+yob+"), tel. "+phone; } public static void main(String[] args) { Phone tel = new Phone("808", "4561414"); Person gwb = new Person("G. W. Bush", true, 1946, tel); System.out.println(gwb); tel.setAreaCode("202"); System.out.println(gwb); } }
Chapter 2
Abstract Data Types
2.1 ADT: Int
package chap02.exer01; public interface Integer { public Integer plus(Integer x); public Integer minus(Integer x); public Integer times(Integer x); }
«interface» Int
+Int(long) +isLessThan(Int) +minus(Int):Int +plus(Int):Int +times(Int):Int