finished implementations of Dog.java, Factorial.java, IsInteger.java,
ThreeStrings.java
This commit is contained in:
parent
9d96d908f6
commit
56d14c93bb
@ -11,16 +11,24 @@ package dog;
|
||||
|
||||
|
||||
public class Dog {
|
||||
private String breed;
|
||||
// Add an instance variable here for name.
|
||||
|
||||
public Dog(String theBreed)
|
||||
// for my sanity (and out of habit), all member variables will be renamed to start with m
|
||||
|
||||
// Member vars
|
||||
|
||||
private String mBreed;
|
||||
private String mName;
|
||||
|
||||
// Constructor
|
||||
public Dog(String breed, String name)
|
||||
{
|
||||
breed = theBreed;
|
||||
breed = mBreed;
|
||||
name = mName;
|
||||
}
|
||||
|
||||
//toString (no @Override?)
|
||||
public String toString()
|
||||
{
|
||||
return breed;
|
||||
return mName + " is a " + mBreed;
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ public class Factorial {
|
||||
public static int factorial(int n)
|
||||
{
|
||||
// Base case
|
||||
if (n < 1) return n;
|
||||
if (n <= 1) return 1;
|
||||
|
||||
// Recursive
|
||||
return n * factorial(n - 1);
|
||||
|
||||
@ -8,12 +8,15 @@ public class IsInteger {
|
||||
// It is an integer if all of the characters in the string are digits.
|
||||
//
|
||||
// Return true if it is an integer, or false if it is not.
|
||||
public static isInteger(String input)
|
||||
public static boolean isAnInteger(String input)
|
||||
{
|
||||
|
||||
// Check if you can convert
|
||||
|
||||
|
||||
try {
|
||||
Integer.parseInt(input);
|
||||
return true;
|
||||
}
|
||||
catch(NumberFormatException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -18,9 +18,47 @@ package threeStrings;
|
||||
//Third string? donuts
|
||||
//go + fish is not equal to donuts!
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ThreeStrings {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
// String vars
|
||||
String string1;
|
||||
String string2;
|
||||
String string3;
|
||||
String state = " is not ";
|
||||
|
||||
// Prompts
|
||||
// First
|
||||
System.out.print("First string? ");
|
||||
string1 = scan.nextLine();
|
||||
|
||||
// Second
|
||||
System.out.print("Second string? ");
|
||||
string2 = scan.nextLine();
|
||||
|
||||
// Third
|
||||
System.out.print("Third string? ");
|
||||
string3 = scan.nextLine();
|
||||
|
||||
// Determinant
|
||||
String concat = string1.concat(string2);
|
||||
if (concat.equals(string3)) {
|
||||
|
||||
state = " is ";
|
||||
|
||||
}
|
||||
|
||||
// Result
|
||||
System.out.println(string1 + " + " + string2 + state + "equal to " + string3 + "!");
|
||||
|
||||
|
||||
|
||||
// Close the scanner
|
||||
scan.close();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user