for i in range(0, 100):*a. for(int i = 0; i < 100; i++) {
def inchesToCm(inches):a. public double static inchesToCm(double inches) {
print('Hello', end=' ')*a. System.out.print("Hello")
def main( ): print("Hello, World.")a. Declare main method to be public.
a = [1, 3, 5] b = (1, 3, 5) c = {1 : 'one', 2 : 'two', 3 : 'three'}
public class Demo { public static void main(String[ ] args) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); System.out.println("Sum: " + (a + b)); } }Here are statements to compile and run this demo program:
> javac Demo.java > java Demo 5 7 Sum: 12
import sys a = sys.argv[0] b = sys.argv[1] print(a, b) sum = int(a) + int(b) print("Sum: ", sum)
# Python Version def get_total_wages(hours_worked, hourly_salary): if hours_worked > 40.0: return hourly_salary * (40.0 + (40.0 - hours_worked) * 1.5) else: return hourly_salary * hours_worked hw = 10.0 hs = 10.0 print(get_total_wages(hw, hs)) // Java Version public class Overtime { public static void main(String[ ] args) { double hoursWorked, hourlySalary, totalWages; hoursWorked = 10.0; hourlySalary = 10.0; System.out.println( getTotalWages(hoursWorked, hourlySalary)); } public static double getHourlySalary( double hoursWorked, double hourlySalary)) { if (hoursWorked > 40.0) { return theHourlySalary * (40.0 + (hoursWorked - 40.0) * 1.5); } else { return theHoursWorked * theHourlySalary; } } }
public class LongSong { public static void main(String[ ] args) { // Print 99 verses of the song. playSong(99); } public static void playSong(int numVerses) { for (int n = numVerses; n >= 1; n--) { System.out.println(n + " bottles of beer on the wall,"); System.out.println(n + " bottles of beer."); System.out.println( "Take one down and pass it around,"); System.out.println((n - 1) + " bottles of beer on the wall.\n"); } } }
// Card Example // Source code file: Card.java // A Card object is a standard poker playing card. public class Card { public int rank; public String suit; public Card(int theRank, String theSuit) { this.rank = theRank; this.suit = theSuit; } public String color( ) { if (this.suit.equals("C") || this.suit.equals("S")) { return "black"; } else { return "red"; } } public String toString( ) { String[ ] symbols = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; return symbols[this.rank] + this.suit; } }
NOTHING: 3 2 6 PAIR: 5 5 1 STRAIGHT: 2 3 4 TRIPLE: 4 4 4Also create the traditional test script test1.py. Finally create a simulation script test3.py that records the number of simulated rolls obtained of each roll type in 100,000 rolls. Here are the theoretical percentages for each roll type:
Roll Type | Fraction | Percentage |
---|---|---|
NOTHING | 96/216 | 44.44% |
PAIR | 90/216 | 41.67% |
STRAIGHT | 24/216 | 11.11% |
TRIPLE | 6/216 | 2.78% |
This section was not discussed in class, and will not be covered on the multiple choice final exam.
In traditional software engineering, instance variables are private. Their values are accessed or modified using getter and setter methods. For example, if the instance variable is age, then the getter is named getAge; the setter is named setAge.
public class Person { private String name; private char gender; private int age; ... }Java Getter Definition:
public int getAge( ) { return age; }Java Getter Invocation:
int a = p.getAge( );Java Setter Definition:
public void setAge(int theAge) { age = theAge; }Java Setter Invocation:
p.setAge(12);