Skip to content

Instantly share code, notes, and snippets.

@technoglot
Last active May 1, 2019 03:03
Show Gist options
  • Save technoglot/fb73ee6543aaabfebdfd0e146fd6a8b4 to your computer and use it in GitHub Desktop.
Save technoglot/fb73ee6543aaabfebdfd0e146fd6a8b4 to your computer and use it in GitHub Desktop.
Collection of various Java programming exercises.
import java.util.Scanner;

public class CheckSubstring {
    
    public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      
      System.out.print("Enter the frist string: ");
      String s1 = input.nextLine();
      
      System.out.print("Enter the second string: ");
      String s2 = input.nextLine();
      
      if(checkSubstring(s1, s2) == true){
        System.out.println("The first string is a substring of the second string");    
      } else { 
        System.out.println("The first string isn't a substring of the second string"); 
      }   
    }
    
    public static boolean checkSubstring(String s1, String s2){
    if (s2.indexOf(s1) >= 0){ 
    
    } else if (s2.indexOf(s1) < 0){ 
      return false; 
    }
    return true;
} 
}
import java.util.Scanner;

public class CheckPassword {

    public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      System.out.print("Enter a password: ");
      String password = input.nextLine();

      if (isPasswordValid(password) == true) {
        System.out.println("The password you entered is valid ");
      } else {
        System.out.println("The password you entered is invalid "); 
      }
    }
    
    public static boolean isPasswordValid(String password){
      //Call methods
      length(password);
      passwordComponents(password);
      containsDigits(password);

      if (length(password) == true &&  passwordComponents(password) == true && containsDigits(password) == true) { 
          return true;   
      } else return false;
    }
                
    public static boolean length(String password){
    int length = password.length();
        
    if (length >= 8){
      return true;
    } else return false;  
    }
   
    public static boolean passwordComponents(String password){
      for (int i = 0; i >= 8; i++){
        if ((!Character.isLetter(i)) && (!Character.isDigit(i))){
          return true;
        } else return false;
      }
    
    public static boolean containsDigits(String password){           
      return true;    
    }
    
  }
}
import java.util.Scanner;

public class ReversedInt {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter an integer: ");
    int num = input.nextInt();
    
    //call method
    reverse(num);
    }
    
    public static void reverse(int num){
    
    while (num != 0) {
      int remainder = num % 10;
      System.out.print(remainder);
      num /= 10;    
    }
    
    }
}
import java.util.Scanner;

public class SumDigits {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter an integer: ");
    int value = input.nextInt();
    
    System.out.println("The sum of the digits is: " + sumDigits(value));
    }
    
    //Sum digits in an integer
    public static int sumDigits(long n){
    int temp = (int)Math.abs(n);
    int sum = 0;
   
    do {
      int remainder = temp % 10;
      sum += remainder;
      temp /= 10;    
    } while(temp != 0);
   
    return sum;   
}}
import java.util.Scanner;

public class Convert {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    
    //Prompt user to enter milliseconds
    System.out.print("Enter milliseconds: ");
    long millis = input.nextLong();
    String s = " ";
    
    //call method 
    System.out.println(convertMillis(millis));
    }
    
    public static String convertMillis(long millis){
    String s = " " ;
    
    int seconds = ((int)millis / 1000) % 60;
    int minutes = (((int)millis / 1000) / 60) % 60;
    int hours = (((int)millis / 1000) / 60) / 60 ;
    
    String secondsS = Integer.toString(seconds);
    String minutesS = Integer.toString(minutes);
    String hoursS = Integer.toString(hours);
    
    s = hoursS + ":" + minutesS + ":" + secondsS;
    
    return s;    
    }
}
public static void main(String[] args) {
  
    //Print distinct numbers
    Scanner input = new Scanner(System.in);
    int[] nums = new int[10];
    System.out.print("Enter ten integers: ");
    
    //Saves the input
    for(int i = 0; i < nums.length; i++){
    nums[i] = input.nextInt();
    }
    
    int distinct = 0;
    int[] distinctNums = new int[10];
    for(int x = 0; x < nums.length; x++){
        
       for(int j = 1; j < distinctNums.length; j++){
          if(nums[x] != nums[j]){
          distinctNums[x] = nums[x]; 
          distinct++;
       } else { }
       }
    }
      
    System.out.println("The number of distinct numbers is: " + distinct);
}
public static void main(String[] args) {
    //Assign grades
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the number of students: ");
    int students =  input.nextInt();
    
    int[] scores = new int[students];
    System.out.print("Enter " + students + " scores: ");
    for(int k = 0; k < scores.length; k++){
      scores[k] = input.nextInt();
    }
    
    int best = scores[0];
    for(int j = 1; j < scores.length; j++){
      if(scores[j] > best) best = scores[j];
    }
    
    String grade;
    for(int i = 0; i < scores.length; i++){
      if(scores[i] >= best - 10){
        grade = "A";     
      } else if(scores[i] >= best - 20){
        grade = "B";     
      } else if(scores[i] >= best - 30){
        grade = "C";     
      } else if(scores[i] >= best - 40){
        grade = "D";     
      } else{
        grade = "F"; 
      }
      System.out.println("Student" + i + " score is " + scores[i] + " and grade is " + grade);    
    }
package findmax;

import java.util.Scanner;

public class FindMax {

     public static void main(String[] args) {
       int n, max;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter number of elements in the array:");
        n = s.nextInt();
        int a[] = new int[n];
        System.out.println("Enter elements of array:");
        for(int i = 0; i < n; i++)
        {
            a[i] = s.nextInt();
        }
        max = a[0];
        for(int i = 0; i < n; i++)
        {
            if(max < a[i])
            {
                max = a[i];
            }
        }
        System.out.println("Maximum value:"+max);
    }
    
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment