Tri One Blog
- Learnings
- Primitive Lesson Learnings
- Objects Learnings
- Boolean If-Then Statement Homework
- Iteration Lesson Homework
- Creating Classes Homework
- Array Homework
public class Main {
public static void main() {
int a = 23;
String b = "Test";
double c = 34.355;
System.out.print("The " + a + " is an integer of " + b + " and a double of " + c);
}
}
Main.main();
boolean a = true;
int b = 5;
double c = 7.5;
int a = 5 + 10;
int b = 20 - 5;
int c = 45 / 3;
int d = 5 * 3;
System.out.println(a + b + c + d);
Video Learning Notes
A byte is just 8 bits. Int is used for integer values. Floats are used for values with few decimal digits, while doubles are used for values with many decimal digits. A boolean is used for false or true values. A char will have a single character. A string will have multiple characters.
byte a = 12;
int b = 5;
float c = 6.3f;
double d = 7.344345542;
boolean e = true;
char f = 'W';
String g = "Hello";
public class WordMatch {
/** The secret string. */
private String secret;
/** Constructs a WordMatch object with the given secret string of lowercase letters. */
/** Returns a score for guess, as described in part (a).
* Precondition: 0 < guess.length() <= secret.length()
*/
public int scoreGuess(String guess) {
int count = 0;
for (int i = 0; i <= secret.length() - guess.length(); i++) {
if (secret.substring(i, i + guess.length()).equals(guess)) {
count++;
}
}
return count * guess.length() * guess.length();
/* to be implemented in part (a) */
}
/** Returns the better of two guesses, as determined by scoreGuess and the rules for a
* tie-breaker that are described in part (b).
* Precondition: guess1 and guess2 contain all lowercase letters.
* guess1 is not the same as guess2.
*/
public String findBetterGuess(String guess1, String guess2) {
/* to be implemented in part (b) */
if (scoreGuess(guess1) > scoreGuess(guess2)) {
return guess1;
}
if (scoreGuess(guess2) > scoreGuess(guess1)) {
return guess2;
}
if (guess1.compareTo(guess2) > 0) {
return guess1;
}
return guess2;
}
}
1. The APCalendar class contains methods used to calculate information about a calendar. You will write two methods of the class.
(a) Write the static method numberOfLeapYears, which returns the number of leap years between year1 and year2, inclusive.
In order to calculate this value, a helper method is provided for you.
- isLeapYear(year) returns true if year is a leap year and false otherwise.
Complete method numberOfLeapYears below. You must use isLeapYear appropriately to receive full credit.
Answer:
/** Returns the number of leap years between year1 and year2, inclusive.
* Precondition: 0 <= year1 <= year2
*/
public static int numberOfLeapYears(int year1, int year2) {}
int count = 0;
for (int i = year1; i <= year2; i++) {
if (isLeapYear(i)) {
count++;
}
return count;
}
}
In this problem I created a loop so that it would go through all the years that are in between year1 and year2. Then inside of that I put a if statement to decide if there was a leap year. If yes, count would be increased by 1.
(b) Write the static method dayOfWeek, which returns the integer value representing the day of the week for the given date (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday. For example, 2019 began on a Tuesday, and January 5 is the fifth day of 2019. As a result, January 5, 2019, fell on a Saturday, and the method call dayOfWeek(1, 5, 2019) returns 6.
As another example, January 10 is the tenth day of 2019. As a result, January 10, 2019, fell on a Thursday, and the method call dayOfWeek(1, 10, 2019) returns 4.
In order to calculate this value, two helper methods are provided for you.
- firstDayOfYear(year) returns the integer value representing the day of the week for the first day of year, where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday. For example, since 2019 began on a Tuesday, firstDayOfYear(2019) returns 2.
- dayOfYear(month, day, year) returns n, where month, day, and year specify the nth day of the year. For the first day of the year, January 1 (month = 1, day = 1), the value 1 is returned. This method accounts for whether year is a leap year. For example, dayOfYear(3, 1, 2017) returns 60, since 2017 is not a leap year, while dayOfYear(3, 1, 2016) returns 61, since 2016 is a leap year.
Class information for this question
- public class APCalendar
- private static boolean isLeapYear(int year)
- public static int numberOfLeapYears(int year1, int year2)
- private static int firstDayOfYear(int year)
- private static int dayOfYear(int month, int day, int year)
- public static int dayOfWeek(int month, int day, int year)
Complete method dayOfWeek below. You must use firstDayOfYear and dayOfYear appropriately to receive full credit.
Answer:
/** Returns the value representing the day of the week for the given date
* (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ...,
* and 6 denotes Saturday.
* Precondition: The date represented by month, day, year is a valid date.
*/
public static int dayOfWeek(int month, int day, int year) {
int firstDay = firstDayOfYear(year);
int day = dayOfYear(month, day, year);
return (firstDay + day - 1) % 7;
}
For this, I created two variables and set them to the firstDayOfYear and dayOfYear. After that, I added the two values together and subtracted 1. The % 7 is what the remainder is.
public class CaesarCipher {
String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
String[] capitalLetters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
static String message1 = "Kfzb gly!";
static String message2 = "zlab zlab zlab";
static String message3 = "prmbozxifcoxdfifpqfzbumfxifalzflrp";
String letter = "";
public CaesarCipher(String message) {
for (int i = 0; i < message.length(); i++) {
letter = message.substring(i, i + 1);
if (letter.equals(" ")) {
System.out.print(" ");
}
if (letter.equals("!")) {
System.out.print("!");
}
for (int j = 0; j < letters.length; j++) {
if (letter.equals(letters[j])) {
System.out.print(letters[(j + 3) % 26]);
}
if (letter.equals(capitalLetters[j])) {
System.out.print(capitalLetters[(j+3)%26]);
}
}
/*String oldLetter = message1.substring(i);
System.out.println(oldLetter);
int oldIndex = Arrays.asList(letters).indexOf(oldLetter);
String newLetter = letters[(oldIndex + 3) % 26];
newMessage += newLetter;
//System.out.println(message1.indexOf(letters[i+3]));
*/
}
System.out.println("");
}
public static void main(String[] args) {
CaesarCipher decode = new CaesarCipher(message1);
CaesarCipher decode2 = new CaesarCipher(message2);
CaesarCipher decode3 = new CaesarCipher(message3);
}
}
CaesarCipher.main(null);
public class StepTracker {
private int minSteps;
private int totalSteps;
private int numDays;
private int numActivateDays;
public StepTracker (int threshold) {
minSteps = threshold;
totalSteps = 0;
numDays = 0;
numActiveDays = 0;
}
public void addDailySteps(int steps) {
totalSteps += steps;
numDays++;
if (steps >= minSteps) {
numActiveDays++;
}
}
public int activeDays() {
return numActiveDays;
}
public double averageSteps() {
if (numDays == 0) {
return 0.0;
}
else {
return (double) totalSteps / numDays;
}
}
}
public void addMembers(String[] names, int gradYear) {
for (String a: names) {
MemberInfo newM = new memberInfo (a, gradYear, true);
memberList.add(newM);
}
}