Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------------------------------------------------------------------------------------------
- // Exercise 2
- /**
- * Created by Daniel on 4/21/2016.
- */
- public class Exercise2 {
- public static void main(String[] args){
- String str = repl("lol",3);
- System.out.println(str);
- }
- public static String repl(String s, int l){
- String str = "";
- for(int i = 1; i <= l; i++){
- str = str + s;
- }
- return str;
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- // Exercise 4
- /**
- * Created by Daniel on 4/21/2016.
- */
- public class Exercise4 {
- public static void main(String[] args) {
- int days = daysInMonth(4);
- System.out.println(days);
- }
- public static int daysInMonth(int m) {
- int n = 31;
- if (m == 2) n = 28;
- if (m == 4 || m == 6 || m == 9 || m == 11) n = 30;
- return n;
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- // Exercise 6
- /**
- * Created by Daniel on 4/21/2016.
- */
- public class Exercise6 {
- public static void main(String[] args){
- printRange(19,11);
- printRange(5,5);
- printRange(2,7);
- }
- public static void printRange(int n1, int n2){
- for(int i = Math.min(n1,n2); i <= Math.max(n1,n2);i++)System.out.print(i + " ");
- System.out.println();
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- // Exercise 8
- /**
- * Created by Daniel on 4/22/2016.
- */
- import java.util.*;
- public class Exercise8 {
- public static void main(String[] args) {
- Scanner c = new Scanner(System.in);
- smallestLargest(c);
- }
- public static void smallestLargest(Scanner c) {
- System.out.print("How many numbers do ou want to enter?");
- int n = c.nextInt();
- int min = 0;
- int max = 0;
- int num = 0;
- if(n<= 0) throw new IllegalArgumentException("You can't enter numbers <= 0!");
- for (int i = 1; i <= n; i++) {
- System.out.printf("Number %d: ", i);
- num = c.nextInt();
- max = Math.max(num,max);
- min = Math.min(num,min);
- }
- System.out.printf("Smallest = %d\n",min);
- System.out.printf("Largest = %d\n",max);
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- // Exercise 10
- /**
- * Created by Daniel on 4/22/2016.
- */
- import com.sun.javaws.exceptions.InvalidArgumentException;
- import java.util.*;
- public class Exercise10 {
- public static void main(String[] args){
- Scanner c = new Scanner(System.in);
- printGPA(c);
- }
- public static void printGPA(Scanner c){
- System.out.print("Enter a student record: ");
- String name = c.next();
- int n = c.nextInt();
- double sum = 0;
- for(int i = 1; i <= n;i++){
- double num = c.nextDouble();
- sum += num;
- }
- sum /= n;
- System.out.printf("%s's grade is %.2f",name,sum);
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- // Exercise 12
- /**
- * Created by Daniel on 4/22/2016.
- */
- public class Exercise12 {
- public static void main(String[] args){
- printTriangleType(5,7,7);
- printTriangleType(6,6,6);
- printTriangleType(5,7,8);
- //printTriangleType(2,18,2); // will throw an exception
- }
- public static void printTriangleType(double s1, double s2, double s3){
- double h = Math.max(s1,s2);
- h = Math.max(h,s3);
- double s = s1 + s2 + s3 - h;
- double m = Math.min(s1,s2);
- m = Math.min(m,s3);
- if(h>s || m<= 0)throw new IllegalArgumentException("Sum of any two sides should not be lower than the third side, and side lengths should not be negative");
- String type;
- if(s1 == s2 && s1 == s3){
- type = "equilateral";
- }else if(s1 == s2 || s1 == s3 || s2 == s3){
- type = "isosceles";
- }else{
- type = "scalene";
- }
- System.out.println(type);
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- // Exercise 14
- /**
- * Created by Daniel on 4/24/2016.
- */
- public class Exercise14 {
- public static void main(String[] args){
- //Println statements to test method
- System.out.println(pow(-4.0,3));
- System.out.println(pow(4.0,-2));
- }
- public static double pow(double n, int p){
- double a = 1;
- for(int i = 1;i<= Math.abs(p);i++){
- if(p>0)a*=n;
- if(p<0)a/=n;
- }
- return a;
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- // Exercise 16
- /**
- * Created by Daniel on 4/24/2016.
- */
- import java.util.*;
- public class Exercise16 {
- public static void main(String[] args) {
- Scanner c = new Scanner(System.in);
- printPalindrome(c);
- }
- public static void printPalindrome(Scanner c) {
- System.out.print("Enter a string : ");
- String s = c.nextLine();
- String b = "";
- for (int i = s.length() - 1; i >= 0; i--) {
- b += s.charAt(i);
- }
- if (s.equalsIgnoreCase(b)) System.out.printf("%s backwards is %s, %s is a palindrome", s, b, s);
- else System.out.printf("%s backwards is %s, %s is not palindrome", s, b, s);
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- // Exercise 18
- /**
- * Created by Daniel on 4/24/2016.
- */
- public class Exercise18 {
- public static void main(String[] args) {
- //example calls
- System.out.println(wordCount("hello"));
- System.out.println(wordCount("how are you?"));
- System.out.println(wordCount(" this string has wide spaces "));
- System.out.println(wordCount(" "));
- }
- public static int wordCount(String s) {
- boolean lastSpace = true;
- boolean Space = false;
- int count = 0;
- for (int i = 0; i < s.length(); i++) {
- if (s.charAt(i) == ' ') Space = true;
- if (!Space && lastSpace) count++;
- lastSpace = Space;
- Space = false;
- }
- return count;
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- // Exercise 20
- /**
- * Created by Daniel on 4/24/2016.
- */
- public class Exercise20 {
- public static void main(String[] args) {
- //println statements to test numUnique
- System.out.println(numUnique(18, 3, 4));
- System.out.println(numUnique(6, 7, 6));
- System.out.println(numUnique(5, 5, 5));
- }
- public static int numUnique(int n1, int n2, int n3) {
- int u = 3;
- if (n1 == n2 && n1 == n3) u = 1;
- else if (n1 == n2 || n1 == n3 || n2 == n3) u = 2;
- return u;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment