Skip to content

Instantly share code, notes, and snippets.

@ilialloyd
Last active September 1, 2022 02:21
Show Gist options
  • Save ilialloyd/f3ae20136a870768a9f9cf6e225048ca to your computer and use it in GitHub Desktop.
Save ilialloyd/f3ae20136a870768a9f9cf6e225048ca to your computer and use it in GitHub Desktop.
Tim Buchalka /Udemy
/**
* Create a method called calcFeetAndInchesToCentimetres, it needs to have 2 parameters (feet, inches).
*
* You should validate that the first parameter feet is >= 0
* You should validate that the second parameter inches is >= 0 and <= 12
* return -1 from the method if either of the above is not true.
*
* If the parameters are valid, then calculate how many centimeters comprise the feet and inches passed
* to this method and return that value.
*
* Create a second method of the same name but with only 1 parameter (inches)
* validate that its >= 0
* return -1 if it's not true.
*
* If its valid, then calculate how many feet are in the inches and here is the tricky part:
* call the other overloaded method passing the correct feet and inches calculated so that it can calculate
* correctly.
*
* Hints:
* Use double for your number datatypes is probably a good idea
* 1 inch = 2.54cm and 1 foot = 12 inches
*/
public class OverloadingExercise {
public static void main(String[] args) {
double inch = 7;
if (!(inch > 0)) {
System.out.println("Invalid number");
}else {
double feet = calcFeetAndInchesToCentimeters(inch);
double sm = calcFeetAndInchesToCentimeters(feet, inch);
System.out.println(inch + " inch = " + feet+" feet");
System.out.println(feet + " feet and " + inch + " inch is: " + sm + " sm");
}
}
public static double calcFeetAndInchesToCentimeters(double feet, double inch){
double sm;
if(!(feet>=0) || !(inch>=0 || inch<=12)){
return -1;
}else {
sm = calcFeetAndInchesToCentimeters(inch)*30.48;
}
return sm;
}
public static double calcFeetAndInchesToCentimeters(double inch){
double inchToFeet=0;
if(!(inch>0)){
return -1;
}else{
inchToFeet =(float) inch/12;
}
return inchToFeet;
}
}
Barking Dog
We have a dog that likes to bark. We need to wake up if the dog is barking at night!
Write a method shouldWakeUp that has 2 parameters.
1st parameter should be of type boolean and be named barking it represents if our dog is currently barking.
2nd parameter represents the hour of the day and is of type int with the name hourOfDay and has a valid range of 0-23.
We have to wake up if the dog is barking before 8 or after 22 hours so in that case return true.
In all other cases return false.
If the hourOfDay parameter is less than 0 or greater than 23 return false.
public class BarkingDog {
public static boolean shouldWakeUp(boolean barking , int hourOfDay){
if (hourOfDay >= 0 && hourOfDay <= 23) { // to check right hour number
if (hourOfDay <= 7 || hourOfDay >= 23) { // to check barking hours
return barking; //flag to true if its during bark hours
}
}else{
return false; //out barking hours entry flag false
}
return false; // flag false if invalid hour
}
}
public class LeapYear {
public static boolean isLeapYear(int year){
if(year<1 || year>9999){
return false;
}else{
if(!(year%4==0)){
return false;
}else{
if(!(year%100==0)){
return true;
}else{
if(!(year%400==0)){
return false;
}
return true;
}
}
}
}
}
public class TeenNumberChecker {
public static boolean hasTeen(int one,int two,int three){
int [] arr = {one,two,three};
for(int i=0;i<arr.length;i++){
if(arr[i]>=13 && arr[i]<=19){
return true;
}
}
return false;
}
public static boolean isTeen(int teen){
if(!(teen>=13 && teen<=19)){
return false;
}
return true;
}
}
public class AreaCalculator {
public static double area(double radius) {
if (radius < 0) {
System.out.println("Invalid value");
return -1;
}
double area = Math.PI * (radius * radius);
return area;
}
public static double area(double x, double y) {
if (x < 0 || y < 0) {
System.out.println("Invalid value");
return -1;
}
double area = x * y;
return area;
}
}
public class DecimalComparator{
public static boolean areEqualByThreeDecimalPlaces(double first, double second) {
if (first <0 && second >0 || first >0 && second <0){
return false;
}
// double first1 = first*1000;
// double second2 = second*1000;
// int i = (int)first1;
// int j = (int)second2;
int i = (int)(first*1000);
int j = (int)(second*1000);
if(i==j){
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment