Skip to content

Instantly share code, notes, and snippets.

@OddExtension5
Last active September 18, 2021 19:52
Show Gist options
  • Save OddExtension5/190b8d4f8b58d5d89a395abac38e49e7 to your computer and use it in GitHub Desktop.
Save OddExtension5/190b8d4f8b58d5d89a395abac38e49e7 to your computer and use it in GitHub Desktop.
DZone JAVA CLASSES

WELCOME PROGRAM

public class Welcome1 {
   // main method begins execution of Java application
   public static void main(String[] args) {
      System.out.println("Welcome to Java Programming!");
   } // end method main
} // end class Welcome1

ADDITION

// Addition program that displays the sum of two numbers.
import java.util.Scanner; // program uses class Scanner

public class Addition {
   // main method begins execution of Java application
   public static void main(String[] args) {
      // create a Scanner to obtain input from the command window
      Scanner input = new Scanner(System.in);

      System.out.print("Enter first integer: "); // prompt 
      int number1 = input.nextInt(); // read first number from user

      System.out.print("Enter second integer: "); // prompt 
      int number2 = input.nextInt(); // read second number from user

      int sum = number1 + number2; // add numbers, then store total in sum

      System.out.printf("Sum is %d%n", sum); // display sum
   } // end method main
} // end class Addition

ACCOUNT

// Account class with a double instance variable balance and a constructor
// and deposit method that perform validation.

public class Account {
   private String name; // instance variable 
   private double balance; // instance variable 

   // Account constructor that receives two parameters  
   public Account(String name, double balance) {
      this.name = name; // assign name to instance variable name

      // validate that the balance is greater than 0.0; if it's not,
      // instance variable balance keeps its default initial value of 0.0
      if (balance > 0.0) { // if the balance is valid
         this.balance = balance; // assign it to instance variable balance
      }
   }

   // method that deposits (adds) only a valid amount to the balance
   public void deposit(double depositAmount) {      
      if (depositAmount > 0.0) { // if the depositAmount is valid
         balance = balance + depositAmount; // add it to the balance 
      }
   }

   // method returns the account balance
   public double getBalance() {
      return balance; 
   } 

   // method that sets the name
   public void setName(String name) {
      this.name = name; 
   } 

   // method that returns the name
   public String getName() {
      return name; 
   } 
}

LETTER GRADES

// LetterGrades class uses the switch statement to count letter grades.
import java.util.Scanner; 

public class LetterGrades {
   public static void main(String[] args) {
      int total = 0; // sum of grades                  
      int gradeCounter = 0; // number of grades entered
      int aCount = 0; // count of A grades             
      int bCount = 0; // count of B grades             
      int cCount = 0; // count of C grades             
      int dCount = 0; // count of D grades             
      int fCount = 0; // count of F grades             

      Scanner input = new Scanner(System.in);

      System.out.printf("%s%n%s%n   %s%n   %s%n", 
         "Enter the integer grades in the range 0-100.", 
         "Type the end-of-file indicator to terminate input:", 
         "On UNIX/Linux/macOS type <Ctrl> d then press Enter",
         "On Windows type <Ctrl> z then press Enter");

      // loop until user enters the end-of-file indicator
      while (input.hasNext()) {
         int grade = input.nextInt(); // read grade
         total += grade; // add grade to total
         ++gradeCounter; // increment number of grades
         
         //  increment appropriate letter-grade counter
         switch (grade / 10) {                          
            case 9:  // grade was between 90              
            case 10: // and 100, inclusive                
               ++aCount;                               
               break; // exits switch                  
            case 8: // grade was between 80 and 89        
               ++bCount;                               
               break; // exits switch                      
            case 7: // grade was between 70 and 79        
               ++cCount;                               
               break; // exits switch                      
            case 6: // grade was between 60 and 69        
               ++dCount;                               
               break; // exits switch                      
            default: // grade was less than 60            
               ++fCount;                               
               break; // optional; exits switch anyway 
         }                                             
      }  

      // display grade report
      System.out.printf("%nGrade Report:%n");

      // if user entered at least one grade...
      if (gradeCounter != 0) {
         // calculate average of all grades entered
         double average = (double) total / gradeCounter;  

         // output summary of results
         System.out.printf("Total of the %d grades entered is %d%n", 
            gradeCounter, total);
         System.out.printf("Class average is %.2f%n", average);
         System.out.printf("%n%s%n%s%d%n%s%d%n%s%d%n%s%d%n%s%d%n", 
            "Number of students who received each grade:", 
            "A: ", aCount,  // display number of A grades
            "B: ", bCount,  // display number of B grades
            "C: ", cCount,  // display number of C grades 
            "D: ", dCount,  // display number of D grades
            "F: ", fCount); // display number of F grades
      }
      else { // no grades were entered, so output appropriate message
         System.out.println("No grades were entered");
      } 
   }
} 

BREAK TEST

// break statement exiting a for statement.
public class BreakTest {
   public static void main(String[] args) {
      int count; // control variable also used after loop terminates
      
      for (count = 1; count <= 10; count++) { // loop 10 times
         if (count == 5) {
            break; // terminates loop if count is 5
         }

         System.out.printf("%d ", count);
      } 

      System.out.printf("%nBroke out of loop at count = %d%n", count);
   }
} 

CONTINUE TEST

// continue statement terminating an iteration of a for statement.
public class ContinueTest {
   public static void main(String[] args) {
      for (int count = 1; count <= 10; count++) { // loop 10 times
         if (count == 5) {
            continue; // skip remaining code in loop body if count is 5
         } 

         System.out.printf("%d ", count);
      } 

      System.out.printf("%nUsed continue to skip printing 5%n");
   } 
} 

LOGIC OPERATORS

// Logical operators.

public class LogicalOperators {
   public static void main(String[] args) {
      // create truth table for && (conditional AND) operator
      System.out.printf("%s%n%s: %b%n%s: %b%n%s: %b%n%s: %b%n%n",
         "Conditional AND (&&)", "false && false", (false && false),
         "false && true", (false && true), 
         "true && false", (true && false),
         "true && true", (true && true));

      // create truth table for || (conditional OR) operator
      System.out.printf("%s%n%s: %b%n%s: %b%n%s: %b%n%s: %b%n%n",
         "Conditional OR (||)", "false || false", (false || false),
         "false || true", (false || true),
         "true || false", (true || false),
         "true || true", (true || true));

      // create truth table for & (boolean logical AND) operator
      System.out.printf("%s%n%s: %b%n%s: %b%n%s: %b%n%s: %b%n%n",
         "Boolean logical AND (&)", "false & false", (false & false),
         "false & true", (false & true),
         "true & false", (true & false),
         "true & true", (true & true));

      // create truth table for | (boolean logical inclusive OR) operator
      System.out.printf("%s%n%s: %b%n%s: %b%n%s: %b%n%s: %b%n%n",
         "Boolean logical inclusive OR (|)",
         "false | false", (false | false),
         "false | true", (false | true),
         "true | false", (true | false),
         "true | true", (true | true));

      // create truth table for ^ (boolean logical exclusive OR) operator
      System.out.printf("%s%n%s: %b%n%s: %b%n%s: %b%n%s: %b%n%n",
         "Boolean logical exclusive OR (^)", 
         "false ^ false", (false ^ false),
         "false ^ true", (false ^ true),
         "true ^ false", (true ^ false),
         "true ^ true", (true ^ true));

      // create truth table for ! (logical negation) operator
      System.out.printf("%s%n%s: %b%n%s: %b%n", "Logical NOT (!)",
         "!false", (!false), "!true", (!true));
   } 
} 

SCOPE

// Scope class demonstrates field and local variable scopes.

public class Scope {
   // field that is accessible to all methods of this class
   private static int x = 1;                               

   // method main creates and initializes local variable x 
   // and calls methods useLocalVariable and useField
   public static void main(String[] args) {
      int x = 5; // method's local variable x shadows field x

      System.out.printf("local x in main is %d%n", x);

      useLocalVariable(); // useLocalVariable has local x
      useField(); // useField uses class Scope's field x
      useLocalVariable(); // useLocalVariable reinitializes local x
      useField(); // class Scope's field x retains its value

      System.out.printf("%nlocal x in main is %d%n", x);
   } 

   // create and initialize local variable x during each call
   public static void useLocalVariable() {
      int x = 25; // initialized each time useLocalVariable is called

      System.out.printf(
         "%nlocal x on entering method useLocalVariable is %d%n", x);
      ++x; // modifies this method's local variable x
      System.out.printf(
         "local x before exiting method useLocalVariable is %d%n", x);
   } 

   // modify class Scope's field x during each call
   public static void useField() {
      System.out.printf(
         "%nfield x on entering method useField is %d%n", x);
      x *= 10; // modifies class Scope's field x
      System.out.printf(
         "field x before exiting method useField is %d%n", x);
   }
} 

METHOD OVERLOAD

// Overloaded method declarations.

public class MethodOverload {
   // test overloaded square methods
   public static void main(String[] args) {
      System.out.printf("Square of integer 7 is %d%n", square(7));
      System.out.printf("Square of double 7.5 is %f%n", square(7.5));
   } 
   
   // square method with int argument                             
   public static int square(int intValue) {
      System.out.printf("%nCalled square with int argument: %d%n",
         intValue);                                               
      return intValue * intValue;                                 
   } 
 
   // square method with double argument                             
   public static double square(double doubleValue) {
      System.out.printf("%nCalled square with double argument: %f%n",
         doubleValue);                                               
      return doubleValue * doubleValue;                              
   } 
} 

PROGRAM 1

Write a program to print the details about you i.e., your name and age using variables.

public clas First {
  public static void main(String args[]){
    String name = "Sushil";
    int age = 22;
    System.out.println("My name is " + name);
    System.out.println("My age is " + age);
  }
}

PROGRAM 2

Write a program to print sum of three given numbers 1221, 2332, 3443.

public class SumOfThreeNumbers {
  public static void main(String args[]) {
    int num1, num2, num3, sum;
    
    num1 = 1221;
    num2 = 2332;
    num3 = 3443;
    sum = num1 + num2 + num3;
    
    System.out.println("Three given numbers are : ");
    System.out.println(num1);
    System.out.println(num2);
    System.out.println(num3);
    System.out.println("Sum = " + sum);
  }
}

PROGRAM 3

Write a program to find the perimeter of a traingle with sides measuring 10cm, 14cm, and 15cm.

public class Perimeter {
  public static void main(String args[]){
    int side1 = 10, side2 = 14, side3 = 15;
    int perimeter;
    
    perimeter = side1 + side2 + side3;
    
    System.out.pritln("Perimeter of triangle with sides 10cm, 14cm, and 15cm is" + perimeter + "cm");
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment