Skip to content

Instantly share code, notes, and snippets.

@bytecodeman bytecodeman/DayOfDate.java Secret
Created Sep 28, 2019

Embed
What would you like to do?
CSC-111 If statement examples and sample code
import java.util.*;
public class DayOfDate {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter year: (e.g., 2008): ");
int year = input.nextInt();
System.out.print("Enter month: 1-12: ");
int month = input.nextInt();
if (month == 1)
{
month = 13;
year = year - 1; // January is counted as month 13 of the previous
// year.
}
else if (month == 2)
{
month = 14;
year = year - 1; // February is counted as month 14 of the previous
// year.
}
System.out.print("Enter the day of the month: 1-31: ");
int dayOfMonth = input.nextInt();
int j = year / 100;
int k = year % 100;
int dayOfWeek = (dayOfMonth + 26 * (month + 1) / 10 + k + k / 4 + j / 4 + 5 * j) % 7;
if (dayOfWeek == 0)
System.out.println("Day of the week is Saturday");
else if (dayOfWeek == 1)
System.out.println("Day of the week is Sunday");
else if (dayOfWeek == 2)
System.out.println("Day of the week is Monday");
else if (dayOfWeek == 3)
System.out.println("Day of the week is Tuesday");
else if (dayOfWeek == 4)
System.out.println("Day of the week is Wednesday");
else if (dayOfWeek == 5)
System.out.println("Day of the week is Thursday");
else if (dayOfWeek == 6)
System.out.println("Day of the week is Friday");
}
}
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a three-digit integer: ");
int number = input.nextInt();
if (number >= 100 && number <= 999) { // && AND Both arguments must be TRUE
int lsd = number % 10;
int msd = number / 100;
if (lsd == msd)
System.out.println("Palindrome!!!");
else
System.out.println("NO Palindrome");
}
else {
System.out.println("Not a 3 Digit Integer.");
}
if (number < 100 || number > 999) // OR means one or the other must be TRUE
System.out.println("Not a 3 Digit Integer.");
else {
int lsd = number % 10;
int msd = number / 100;
if (lsd == msd)
System.out.println("Palindrome!!!");
else
System.out.println("NO Palindrome");
}
input.close();
}
}
public class PickACardWithIF {
public static void main(String[] args) {
int card = (int)(52 * Math.random());
int suit = card / 13;
int rank = card % 13;
String strRank;
if (rank == 0)
strRank = "Ace";
else if (rank >= 1 && rank <= 9)
strRank = "" + (rank + 1);
else if (rank == 10)
strRank = "Jack";
else if (rank == 11)
strRank = "Queen";
else
strRank = "King";
String strSuit;
if (suit == 0)
strSuit = "Spades";
else if (suit == 1)
strSuit = "Hearts";
else if (suit == 2)
strSuit = "Clubs";
else
strSuit = "Diamonds";
System.out.println(strRank + " of " + strSuit);
}
}
public class PickACardWithSwith {
public static void main(String[] args) {
int card = (int)(52 * Math.random());
int suit = card / 13;
int rank = card % 13;
String strRank = "";
switch (rank) {
case 0:
strRank = "Ace";
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
strRank = "" + (rank + 1);
break;
case 10:
strRank = "Jack";
break;
case 11:
strRank = "Queen";
break;
case 12:
strRank = "King";
break;
}
String strSuit = "";
switch (suit) {
case 0:
strSuit = "Spades";
break;
case 1:
strSuit = "Hearts";
break;
case 2:
strSuit = "Clubs";
break;
case 3:
strSuit = "Diamonds";
break;
}
System.out.println(strRank + " of " + strSuit);
}
}
import java.util.Scanner;
public class Quadratic {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a, b, c: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
if (a == 0) {
System.out.println("Sorry. a == 0 You don't have a quadratic.");
}
else {
double discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
System.out.println("The equation has no real roots");
}
else if (discriminant == 0) {
double r = -b / (2 * a);
System.out.println("The equation has one root " + r);
}
else {
double r1 = (-b + Math.sqrt(discriminant) / (2 * a));
double r2 = (-b - Math.sqrt(discriminant) / (2 * a));
System.out.println("The equation has two roots " + r1 + " and " + r2);
}
}
input.close();
}
public static void mainxxx(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a, b, c: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double discriminant = b * b - 4 * a * c;
if (discriminant < 0)
{
System.out.println("The equation has no real roots");
}
else if (discriminant == 0)
{
double r1 = -b / (2 * a);
System.out.println("The equation has one root " + r1);
}
else
{ // (discriminant > 0)
double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
double r2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);
System.out.println("The equation has two roots " + r1 + " and " + r2);
}
}
}
import java.util.Scanner;
public class RandomMonth {
public static void main(String[] args) {
int number = (int)(12 * Math.random()) + 1; // 1 .. 12
Scanner input = new Scanner(System.in);
System.out.println("Enter number: ");
number = input.nextInt();
switch (number) {
case 1:
case -1:
System.out.println("January");
break;
case 2:
case -2:
System.out.println("Feburary");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Bad Month");
break;
}
/*
if (number == 1) {
System.out.println("January");
System.out.println("It's cold");
}
else if (number == 2) {
System.out.println("Feburary");
}
else if (number == 3)
System.out.println("March");
else if (number == 4)
System.out.println("April");
else if (number == 5)
System.out.println("May");
else if (number == 6)
System.out.println("June");
else if (number == 7)
System.out.println("July");
else if (number == 8)
System.out.println("August");
else if (number == 9)
System.out.println("September");
else if (number == 10)
System.out.println("October");
else if (number == 11)
System.out.println("November");
else if (number == 12)
System.out.println("December");
else
System.out.println("Bad Month");
*/
}
public static void mainxxx(String[] args) {
int number = (int) (Math.random() * 12) + 1;
// or int number = (int)(System.currentTimeMillis() % 12 + 1);
// or int number = (int)(Math.random() * 12) + 1;
}
}
import java.util.Scanner;
public class Sort3Numbers1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 3 numbers: ");
int numb1 = input.nextInt();
int numb2 = input.nextInt();
int numb3 = input.nextInt();
if (numb1 > numb2) {
int temp = numb1;
numb1 = numb2;
numb2 = temp;
}
if (numb2 > numb3) {
int temp = numb2;
numb2 = numb3;
numb3 = temp;
}
if (numb1 > numb2) {
int temp = numb1;
numb1 = numb2;
numb2 = temp;
}
System.out.printf("Sorted numbers: %d %d %d\n", numb1, numb2, numb3);
}
}
import java.util.Scanner;
public class Sort3Numbers2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 3 numbers: ");
int numb1 = input.nextInt();
int numb2 = input.nextInt();
int numb3 = input.nextInt();
if (numb1 > numb2) {
int temp = numb1;
numb1 = numb2;
numb2 = temp;
}
if (numb1 > numb3) {
int temp = numb1;
numb1 = numb3;
numb3 = temp;
}
if (numb2 > numb3) {
int temp = numb2;
numb2 = numb3;
numb3 = temp;
}
System.out.printf("Sorted numbers: %d %d %d\n", numb1, numb2, numb3);
}
}
import java.util.Scanner;
public class WindChill {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Enter the temperature in Fahrenheit
System.out.print("Enter the temperature in Fahrenheit: ");
double fahrenheit = input.nextDouble();
if (fahrenheit < -58 || fahrenheit > 41) {
System.out.println("Temperature must be between -58�F and 41�F");
}
else {
// Enter the wind speed miles per hour
System.out.print("Enter the wind speed miles per hour: ");
double speed = input.nextDouble();
if (speed < 2)
{
System.out.println("Speed must be greater than or equal to 2");
}
else {
// Compute wind chill index
double windChillIndex = 35.74 + 0.6215 * fahrenheit - 35.75 * Math.pow(speed, 0.16)
+ 0.4275 * fahrenheit * Math.pow(speed, 0.16);
// Display the result
System.out.println("The wind chill index is " + windChillIndex);
}
}
}
public static void mainxxx(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
// Enter the temperature in Fahrenheit
System.out.print("Enter the temperature in Fahrenheit: ");
double fahrenheit = input.nextDouble();
if (fahrenheit < -58 || fahrenheit > 41)
{
System.out.println("Temperature must be between -58�F and 41�F");
System.exit(1);
}
// Enter the wind speed miles per hour
System.out.print("Enter the wind speed miles per hour: ");
double speed = input.nextDouble();
if (speed < 2)
{
System.out.println("Speed must be greater than or equal to 2");
System.exit(2);
}
// Compute wind chill index
double windChillIndex = 35.74 + 0.6215 * fahrenheit - 35.75 * Math.pow(speed, 0.16)
+ 0.4275 * fahrenheit * Math.pow(speed, 0.16);
// Display the result
System.out.println("The wind chill index is " + windChillIndex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.