Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 2
- /**
- * Created by Daniel on 5/6/2016.
- */
- public class exercise2 {
- public static void main(String[] args){
- System.out.println(GDC(64,8));
- }
- public static int GDC(int a, int b){
- int n = 0;
- for(int i = Math.max(Math.abs(a),Math.abs(b));i>=1;i--)if(a%i == 0&& b%i == 0)n = Math.max(n,i);
- return n;
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 4
- /**
- * Created by Daniel on 5/6/2016.
- */
- import java.util.*;
- public class exercise4 {
- public static void main(String[] args){
- randomX();
- }
- public static void randomX(){
- Random r = new Random();
- int random = 0;
- while(!(random>=16)){
- random = r.nextInt(16) + 5;
- for(int i = 1; i <= random; i++){
- System.out.print("*");
- }
- System.out.println();
- }
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 6
- /**
- * Created by Daniel on 5/6/2016.
- */
- import java.util.*;
- public class exercise6 {
- public static void main(String[] args){
- makeGuesses();
- }
- public static void makeGuesses(){
- int g = 0;
- int c = 0;
- Random r = new Random();
- while(g<48){
- g = r.nextInt(50)+1;
- c++;
- System.out.printf("guess = %d\n",g);
- }
- System.out.printf("total guesses = %d\n",c);
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 8
- /**
- * Created by Daniel on 5/8/2016.
- */
- import java.util.*;
- public class exercise8 {
- public static void main(String[] args){
- randomWalk();
- }
- public static void randomWalk(){
- Random r = new Random();
- int p = 0;
- int m = 0;
- while(Math.abs(p)<3){
- if(r.nextInt(2)==1)p+=1;
- else p-=1;
- System.out.printf("position = %2d\n", p);
- m = Math.max(m,p);
- }
- System.out.printf("max position = %2d",m);
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 10
- /**
- * Created by Daniel on 5/8/2016.
- */
- import java.util.*;
- public class exercise10 {
- public static void main(String[] args){
- hopscotch(3);
- }
- public static void hopscotch(int h){
- if(h>=0)System.out.println(" 1 ");
- int n = 1;
- for(int i = 1; i<=h; i++){
- n++;
- int s = new String("" + n).length() + new String("" +(n+1)).length();
- System.out.print(n);
- for(int j = 1;j<=7-s; j++)System.out.print(" ");
- System.out.println(n+1);
- n+=2;
- System.out.printf("%4d\n",n);
- }
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 12
- /**
- * Created by Daniel on 5/8/2016.
- */
- import java.util.*;
- public class exercise12 {
- public static void main(String[] args){
- Scanner s = new Scanner(System.in);
- printAverage(s);
- }
- public static void printAverage(Scanner s){
- double n = 0;
- int c = 0;
- int m = 0;
- System.out.print("Type a number: ");
- n = s.nextDouble();
- while(n>=0){
- m+= n;
- c++;
- System.out.print("Type a number: ");
- n = s.nextDouble();
- }
- if(c>0)System.out.println("Average was " + (m/c));
- else System.out.println("Average was 0");
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 14
- /**
- * Created by Daniel on 5/8/2016.
- */
- public class Exercise14 {
- public static void main(String[] args){
- System.out.println(hasMidpoint(2,5,8));
- }
- public static boolean hasMidpoint(int n1,int n2, int n3){
- int small = Math.min(n1,Math.min(n2,n3));
- int big = Math.max(n1,Math.max(n2,n3));
- int mid = n1 + n2 + n3 -big - small;
- return ((big -small)/2 + small == mid);
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 16
- /**
- * Created by Daniel on 5/8/2016.
- */
- public class exercise16 {
- public static void main(String[] args){
- System.out.println(anglePairs(120,60,30));
- }
- public static boolean anglePairs(int n1, int n2, int n3){
- return (n1 + n2 == 180 || n1+n3 == 180 || n2+n3 ==180) && (n1+n2 == 90||n1+n3 == 90||n2+n3==90);
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 18
- /**
- * Created by Daniel on 5/8/2016.
- */
- public class exercise18 {
- public static void main(String[] args){
- System.out.println(digitSum(29107));
- }
- public static int digitSum(int n){
- int s = 0;
- while(Math.abs(n)>0){
- s += n%10;
- n/=10;
- }
- return s;
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 20
- /**
- * Created by Daniel on 5/8/2016.
- */
- public class exercise20 {
- public static void main(String[] args){
- System.out.println(digitRange(6));
- System.out.println(digitRange(68437));
- }
- public static int digitRange(int n){
- int max = 0;
- int min = 10;
- while(Math.abs(n)>0){
- max = Math.max(max, n%10);
- min = Math.min(min, n%10);
- n/=10;
- }
- return (max - min +1);
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 22
- /**
- * Created by Daniel on 5/8/2016.
- */
- public class exercise22 {
- public static void main(String args[]){
- System.out.println(allDigitsOdd(135319));
- System.out.println(allDigitsOdd(915213));
- }
- public static boolean allDigitsOdd(int n){
- while(n>0){
- if((n%10)%2 == 0) return false;
- n/=10;
- }
- return true;
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 24
- /**
- * Created by Daniel on 5/8/2016.
- */
- public class exercise24 {
- public static void main(String[] args){
- System.out.println(isAllVowels("eIEiO"));
- System.out.println(isAllVowels("oink"));
- System.out.println(isAllVowels(" "));
- }
- public static boolean isAllVowels(String s) {
- for (int i = 1; i <= s.length(); i++) {
- if (Character.toLowerCase(s.charAt(i - 1)) != 'a' && Character.toLowerCase(s.charAt(i - 1)) != 'e'&& Character.toLowerCase(s.charAt(i - 1)) != 'i' && Character.toLowerCase(s.charAt(i - 1)) != 'o' && Character.toLowerCase(s.charAt(i - 1)) != 'u'&& Character.toLowerCase(s.charAt(i - 1)) != ' ') {//really long if statement
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment