Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


java basics exercises with solution !! filter_list
Author
Message
java basics exercises with solution !! #1
i'll be sharing here some really simple java exercises with solution to help you practice while learning java (only for beginners)

i'll start today by sharing the first two exercises !!

1.Write a program called Sum10 that calculates the sum of the first ten
positive integers 1 + 2 + … + 10.


Spoiler:
Code:
public class Sum10{
    public static void main(String[]args){
       int sum=0;
        for(int i=1;i<=10;i++){
            sum+=i;
        }
        System.out.println("the sum of the first ten positive integers : " +sum);
    }
}


2.Write a program named calculation that reads on the keyboard two inserted integers, calculates their sum, their difference and their product and
displays the results.


Spoiler:
Code:
import java.util.Scanner;
        
public class Calculation{
    public static void main(String[]args){
        Scanner in=new Scanner(System.in);
        int sum,difference,product;
        System.out.println("insert first integer :");
        int nbr1=in.nextInt();
        System.out.println("insert second integer :");
        int nbr2=in.nextInt();
        sum=nbr1+nbr2;
        product=nbr1*nbr2;
        difference=nbr1-nbr2;
        System.out.println("the sum of the integers is : " +sum);
        System.out.println("the difference of the integers is : " +difference);
        System.out.println("the product of the integers is : " +product);
      
        
    }
}
(This post was last modified: 07-16-2014, 01:19 AM by LordPankake.)
[Image: blackeagle_zps6ad86521.gif]

[+] 1 user Likes blackeagle's post
Reply

RE: java basics exercises with solution !! #2
3.Write a program called Convert for inserting a
distance in kilometers from the keyboard and convert it into miles (a mile is 1609 km).


Spoiler:
Code:
import java.util.Scanner;
public class Convert{
    public static void main(String[]aargs){
        Scanner in=new Scanner(System.in);
        System.out.println("insert a distance in kilometers : ");
        Double kilometers=in.nextDouble();
        Double miles=kilometers*1.609;
        System.out.println(kilometers+ " kilometers is equal to " +miles+ " miles");
    }
}


4.Write Java program to allow the user to input his/her age. Then the program will show if the person is eligible to vote. A person who is eligible to vote must be older than or equal to 18 years old.
Enter your age: 18
You are eligible to vote.


Spoiler:
Code:
import java.util.Scanner;
public class Vote{
    public static void main(String[]args){
Scanner in=new Scanner(System.in);
System.out.println("how old are you");
int age;
age=in.nextInt();
if (age>=18){System.out.println("You are eligible to vote.");
    }
else {System.out.println("You are not eligibe to vote.");}
    }}


5.Write a Java program to determine whether an input number is an even number.

Spoiler:
Code:
package even;

import java.util.Scanner;

public class Even {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num;
        System.out.println("enter a number :");
        num = in.nextInt();
        if ((num % 2) == 0) {
            System.out.println("It is an even number.");
        } else {
            System.out.println("It is an odd number.");
        }




    }
}


6.Write a Java program that determines a student’s grade.
The program will read three types of scores(quiz, mid-term, and final scores) and determine the grade based on the following rules:
-if the average score >=90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F
See the example output below:
Quiz score: 80
Mid-term score: 68
Final score: 90
Your grade is B.

Spoiler:
Code:
package grade;

import java.util.Scanner;

public class Grade {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int quiz;
        int midterm;
        int finalscore;
        int average;
        System.out.println("insert quiz grade : ");
        quiz = in.nextInt();
        System.out.println("insert mid-term grade : ");
        midterm = in.nextInt();
        System.out.println("insert final grade : ");
        finalscore = in.nextInt();
        average = (quiz + midterm + finalscore) / 3;
        if (average >= 90) {
            System.out.println("grade=A");
        }
        if ((average >= 70) && (average < 90)) {
            System.out.println("grade=B");
        }
        if ((average >= 50) && (average < 70)) {
            System.out.println("grade=C");
        }
        if (average < 50) {
            System.out.println("grade=F");
        }

    }
}


7.Write a Java program to calculate the revenue from a sale based on the unit price and quantity of a product input by the user.

The discount rate is 10% for the quantity purchased between 100 and 120 units, and 15% for the quantity purchased greater than 120 units. If the quantity purchased is less than 100 units, the discount rate is 0%. See the example output as shown below:
Enter unit price: 25

Enter quantity: 110

The revenue from sale: 275.0$

After discount: 2475.0$(10.0%)


Spoiler:
Code:
package revenue;

import java.util.Scanner;

public class Revenue {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int unitprice, quantity;
        double revenue, afterdiscount, discount = 0;
        System.out.println("Enter unit price:");
        unitprice = in.nextInt();
        System.out.println("Enter quantity:");
        quantity = in.nextInt();
        revenue = unitprice * quantity;
        System.out.println("The revenue from sale: " + revenue + "$");
        if ((quantity >= 100) && (quantity <= 120)) {
            discount = (revenue * 10) / 100;
        }
        if (quantity > 120) {
            discount = (revenue * 15) / 100;
        }
        if (quantity < 100) {
            discount = (revenue * 0) / 100;

        }
        afterdiscount = revenue - discount;
        System.out.println("After discount: " + afterdiscount + " $");

    }
}


8.Write a Java program to detect key presses.(use switch)

If the user pressed number keys( from 0 to 9), the program will tell the number that is pressed, otherwise, program will show "Not allowed".


Spoiler:
Code:
package keypress;

import java.util.Scanner;

public class Keypress {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("press a key : ");
        char key = in.next().charAt(0);
        switch (key) {
            case '0':
                System.out.println("you pressed 0. ");
                break;
            case '1':
                System.out.println("you pressed 1. ");
                break;
            case '2':
                System.out.println("you pressed 2. ");
                break;
            case '3':
                System.out.println("you pressed 3. ");
                break;
            case '4':
                System.out.println("you pressed 4. ");
                break;
            case '5':
                System.out.println("you pressed 5. ");
                break;
            case '6':
                System.out.println("you pressed 6. ");
                break;
            case '7':
                System.out.println("you pressed 7. ");
                break;
            case '8':
                System.out.println("you pressed 8. ");
                break;
            case '9':
                System.out.println("you pressed 9. ");
                break;
            default:
                System.out.println("Not Allowed");
        }

    }
}


9. Write a Java program that allows the user to choose the correct answer of a question.(use switch)

See the example below:
What is the correct way to declare a variable to store an integer value in Java?
a. int 1x=10;
b. int x=10;
c. float x=10.0f;
d. string x="10";
Enter your choice: c


Spoiler:
Code:
package correctanswer;

import java.util.Scanner;

public class CorrectAnswer {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("What is the correct way to declare a variable to store an integer value in Java?");
        System.out.println("a. int 1x=10;");
        System.out.println("b. int x=10;");
        System.out.println("c. float x=10.0f;");
        System.out.println("d. string x= \"10\";");
        System.out.print("Enter ur choice : ");
        char choice = in.next().charAt(0);
        switch (choice) {
            case 'a':
                System.out.println("wrong answer.");
                break;
            case 'b':
                System.out.println("correct answer.");
                break;
            case 'c':
                System.out.println("wrong answer.");
                break;
            case 'd':
                System.out.println("wrong answer.");
                break;
            default:
                System.out.println("your choice " + choice + " dosen't exist .");

        }
    }
}


10.By using do while loop, write Java program to prompt the user to choose the correct answer from a list of answer choices of a question.

The user can choose to continue answering the question or stop answering it. See the example below:
What is the command keyword to exit a loop in Java?

a. int

b. continue

c. break

d. exit

Enter your choice: b

Incorrect!

Again? press y to continue:


Spoiler:
Code:
package exitloop2;

import java.util.Scanner;

public class ExitLoop2 {

    public static void main(String[] args) {
        char repeat;
        do {
            Scanner in = new Scanner(System.in);
            System.out.println("What is the command keyword to exit a loop in Java?");
            System.out.println("a.int");
            System.out.println("b.continue");
            System.out.println("c.break");
            System.out.println("d.exit");
            System.out.print("Enter your choice: ");
            char choice = in.next().charAt(0);
            if ((choice == 'a') || (choice == 'b') || (choice == 'd')) {
                System.out.println("Incorrect!");
                System.out.println("Again? press y to continue: ");
                repeat = in.next().charAt(0);
            } else {

                System.out.println("Correct !");
                repeat = 'n';
            }
        } while (repeat == 'y');

    }
}


11.By using while loop Write Java program to prompt the user to choose the correct answer from a list of answer choices of a question.

The user can choose to continue answering the question or stop answering it. See the example below:
What is the command keyword to exit a loop in Java?

a. int

b. continue

c. break

d. exit

Enter your choice: b

Incorrect!

Again? press y to continue:


Spoiler:
Code:
package exitloop;

import java.util.Scanner;

public class ExitLoop {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char repeat = 'y';
        while (repeat == 'y') {
            System.out.println("What is the command keyword to exit a loop in Java?");
            System.out.println("a.int");
            System.out.println("b.continue");
            System.out.println("c.break");
            System.out.println("d.exit");
            System.out.print("Enter your choice:");
            char answer = in.next().charAt(0);
            if ((answer == 'a') || (answer == 'b') || (answer == 'd')) {
                System.out.println("Incorrect !");
                System.out.println("Again? press y to continue: ");
                repeat = in.next().charAt(0);
            } else {
                System.out.println("Correct!");
                repeat = 'n';
            }
        }

    }
}


12.Using a while loop Write a program that asks the user to enter a character then the program displays the character. So on! The program stops only if the user enters the character 't'.

Spoiler:
Code:
package enterchar2;

import java.util.Scanner;

public class EnterChar2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char character = 'a';
        while (character != 't') {
            System.out.println("Insert a char: ");
            character = in.next().charAt(0);
            System.out.println("Your character is: " + character);
        }
    }
}


13.Using do while loop Write a program that asks the user to enter a character then the program displays the character. So on! The program stops only if the user enters the character 't'.

Spoiler:
Code:
package enterchar;

import java.util.Scanner;

public class EnterChar {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char character;
        do {
            System.out.println("Insert a char: ");
            character = in.next().charAt(0);
            System.out.println("Your character is: " + character);
        } while (character != 't');
    }
}


14.Given an array of ints, return true if 6 appears as either the first or last element in the array. The array will be length 1 or more.

firstLast6({1, 2, 6}) → true
firstLast6({6, 1, 2, 3}) → true
firstLast6({13, 6, 1, 2, 3}) → false


Spoiler:
Code:
public boolean firstLast6(int[] nums) {
if((nums[0]==6)||(nums[(nums.length)-1])==6){
        return true;
}
else {return false ;  
        }
  
}


15.Given an array of ints, return true if the array is length 1 or more, and the first element and the last element are equal.

sameFirstLast({1, 2, 3}) → false
sameFirstLast({1, 2, 3, 1}) → true
sameFirstLast({1, 2, 1}) → true


Spoiler:
Code:
public boolean sameFirstLast(int[] nums) {
    if ((nums.length >= 1) && (nums[0]) == (nums[nums.length - 1])) {
        return true;
    } else {
        return false;
    }
}


16.Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}.

makePi() → {3, 1, 4}


Spoiler:
Code:
public int[] makePi() {
        int t[] = new int[3];
        t[0] = 3;
        t[1] = 1;
        t[2] = 4;
        return t;
    }


17.Given 2 arrays of ints, a and b, return true if they have the same first element or they have the same last element. Both arrays will be length 1 or more.

commonEnd({1, 2, 3}, {7, 3}) → true
commonEnd({1, 2, 3}, {7, 3, 2}) → false
commonEnd({1, 2, 3}, {1, 3}) → true


Spoiler:
Code:
public boolean commonEnd(int[] a, int[] b) {
        if ((a[0] == b[0]) || (a[a.length - 1] == b[b.length - 1])) {
            return true;
        } else {
            return false;
        }
    }


18.Given an array of ints length 3, return the sum of all the elements.

sum3({1, 2, 3}) → 6
sum3({5, 11, 2}) → 18
sum3({7, 0, 0}) → 7


Spoiler:
Code:
public int sum3(int[] nums) {
        int sum = 0;
        sum = nums[0] + nums[1] + nums[2];
        return sum;

    }


19.Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.

rotateLeft3({1, 2, 3}) → {2, 3, 1}
rotateLeft3({5, 11, 9}) → {11, 9, 5}
rotateLeft3({7, 0, 0}) → {0, 0, 7}


Spoiler:
Code:
public int[] rotateLeft3(int[] nums) {
        int temp, temp1;
        temp = nums[2];
        nums[2] = nums[0];
        temp1 = nums[1];
        nums[1] = temp;
        nums[0] = temp1;
        return nums;
    }


20.Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}.

reverse3({1, 2, 3}) → {3, 2, 1}
reverse3({5, 11, 9}) → {9, 11, 5}
reverse3({7, 0, 0}) → {0, 0, 7}


Spoiler:
Code:
public int[] reverse3(int[] nums) {
        int temp;
        temp = nums[0];
        nums[0] = nums[2];
        nums[2] = temp;
        return nums;
    }


21.Given an array of ints length 3, figure out which is larger between the first and last elements in the array, and set all the other elements to be that value. Return the changed array.

maxEnd3({1, 2, 3}) → {3, 3, 3}
maxEnd3({11, 5, 9}) → {11, 11, 11}
maxEnd3({2, 11, 3}) → {3, 3, 3}


Spoiler:
Code:
public int[] maxEnd3(int[] nums) {
        if (nums[0] >= nums[2]) {
            nums[1] = nums[0];
            nums[2] = nums[0];
        }
        if (nums[2] > nums[0]) {
            nums[0] = nums[2];
            nums[1] = nums[2];
        }
        return nums;
    }


22.Given an array of ints, return the sum of the first 2 elements in the array. If the array length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0.

sum2({1, 2, 3}) → 3
sum2({1, 1}) → 2
sum2({1, 1, 1, 1}) → 2


Spoiler:
Code:
public int sum2(int[] nums) {
        int sum = 0;
        if (nums.length == 0) {
            return sum;
        }
        if (nums.length >= 2) {
            sum = nums[0] + nums[1];
        }
        if (nums.length == 1) {
            sum = nums[0];
        }
        return sum;
    }


23.Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements.

middleWay({1, 2, 3}, {4, 5, 6}) → {2, 5}
middleWay({7, 7, 7}, {3, 8, 0}) → {7, 8}
middleWay({5, 2, 9}, {1, 4, 5}) → {2, 4}


Spoiler:
Code:
public int[] middleWay(int[] a, int[] b) {
        int t[] = {a[1], b[1]};
        return t;
    }


24.Given an array of ints, return a new array length 2 containing the first and last elements from the original array. The original array will be length 1 or more.

makeEnds({1, 2, 3}) → {1, 3}
makeEnds({1, 2, 3, 4}) → {1, 4}
makeEnds({7, 4, 6, 2}) → {7, 2}


Spoiler:
Code:
public int[] makeEnds(int[] nums) {
        int[] t = new int[2];
        if (nums.length >= 2) {
            t[0] = nums[0];
            t[1] = nums[nums.length - 1];
        }
        if (nums.length == 1) {
            t[0] = t[1] = nums[0];
        }
        return t;
    }


25.Given an int array length 2, return true if it contains a 2 or a 3.

has23({2, 5}) → true
has23({4, 3}) → true
has23({4, 5}) → false


Spoiler:
Code:
public boolean has23(int[] nums) {
        if ((nums[0] == 2) || (nums[0] == 3)) {
            return true;
        }
        if ((nums[1] == 2) || (nums[1] == 3)) {
            return true;
        } else {
            return false;
        }
    }


26.Given an int array length 2, return true if it does not contain a 2 or 3.

no23({4, 5}) → true
no23({4, 2}) → false
no23({3, 5}) → false


Spoiler:
Code:
public boolean no23(int[] nums) {
        if ((nums[0] == 2) || (nums[1] == 3)) {
            return false;
        }
        if ((nums[1] == 2) || (nums[0] == 3)) {
            return false;
        } else {
            return true;
        }
    }


27.Given an int array, return a new array with double the length where its last element is the same as the original array, and all the other elements are 0. The original array will be length 1 or more. Note: by default, a new int array contains all 0's.

makeLast({4, 5, 6}) → {0, 0, 0, 0, 0, 6}
makeLast({1, 2}) → {0, 0, 0, 2}
makeLast({3}) → {0, 3}


Spoiler:
Code:
public int[] makeLast(int[] nums) {
        int t[] = new int[nums.length * 2];
        t[t.length - 1] = nums[nums.length - 1];
        return t;
    }


28.Given an int array, return true if the array contains 2 twice, or 3 twice. The array will be length 0, 1, or 2.

double23({2, 2}) → true
double23({3, 3}) → true
double23({2, 3}) → false


Spoiler:
Code:
public boolean double23(int[] nums) {
        if (nums.length == 2) {
            if ((nums[0] == 2) && (nums[1] == 2)) {
                return true;
            }
            if ((nums[0] == 3) && (nums[1] == 3)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }


29.Start with 2 int arrays, a and b, of any length. Return how many of the arrays have 1 as their first element.

start1({1, 2, 3}, {1, 3}) → 2
start1({7, 2, 3}, {1}) → 1
start1({1, 2}, {}) → 1


Spoiler:
Code:
public int start1(int[] a, int[] b) {
        int counter = 0;
        if ((a.length == 0) && (b.length == 0)) {
            counter = 0;
        } else if ((a.length >= 1) && (b.length >= 1)) {
            if ((a[0] == 1) || (b[0] == 1)) {
                counter = 1;
            }
            if ((a[0] == 1) && (b[0] == 1)) {
                counter = 2;
            }
            if ((a[0] != 1) && (b[0] != 1)) {
                counter = 0;
            }
        } else if (a.length == 0) {
            if (b[0] == 1) {
                counter = 1;
            } else {
                counter = 0;
            }
        } else if (b.length == 0) {
            if (a[0] == 1) {
                counter = 1;
            } else {
                counter = 0;
            }
        }

        return counter;
    }


30.Start with 2 int arrays, a and b, each length 2. Consider the sum of the values in each array. Return the array which has the largest sum. In event of a tie, return a.

biggerTwo({1, 2}, {3, 4}) → {3, 4}
biggerTwo({3, 4}, {1, 2}) → {3, 4}
biggerTwo({1, 1}, {1, 2}) → {1, 2}


Spoiler:
Code:
public int[] biggerTwo(int[] a, int[] b) {
        int sum1 = 0, sum2 = 0;
        sum1 = a[0] + a[1];
        sum2 = b[0] + b[1];
        if ((sum1 > sum2) || (sum1 == sum2)) {
            return a;
        }
        return b;

    }


31.Given an array of ints of even length, return a new array length 2 containing the middle two elements from the original array. The original array will be length 2 or more.

makeMiddle({1, 2, 3, 4}) → {2, 3}
makeMiddle({7, 1, 2, 3, 4, 9}) → {2, 3}
makeMiddle({1, 2}) → {1, 2}


Spoiler:
Code:
public int[] makeMiddle(int[] nums) {
        int t[] = new int[2];
        if (nums.length == 2) {
            t[0] = nums[0];
            t[1] = nums[1];
        }
        if (nums.length > 2) {
            t[0] = nums[nums.length / 2 - 1];
            t[1] = nums[nums.length / 2];
        }
        return t;
    }


32.Given 2 int arrays, each length 2, return a new array length 4 containing all their elements.

plusTwo({1, 2}, {3, 4}) → {1, 2, 3, 4}
plusTwo({4, 4}, {2, 2}) → {4, 4, 2, 2}
plusTwo({9, 2}, {3, 4}) → {9, 2, 3, 4}


Spoiler:
Code:
public int[] plusTwo(int[] a, int[] b) {
        int t[] = new int[4];
        t[0] = a[0];
        t[1] = a[1];
        t[2] = b[0];
        t[3] = b[1];
        return t;
    }


33.Given an array of ints, swap the first and last elements in the array. Return the modified array. The array length will be at least 1.

swapEnds({1, 2, 3, 4}) → {4, 2, 3, 1}
swapEnds({1, 2, 3}) → {3, 2, 1}
swapEnds({8, 6, 7, 9, 5}) → {5, 6, 7, 9, 8}


Spoiler:
Code:
public int[] swapEnds(int[] nums) {
        int temp;
        if (nums.length > 1) {
            temp = nums[0];
            nums[0] = nums[nums.length - 1];
            nums[nums.length - 1] = temp;
            return nums;
        }
        return nums;
    }


34.Given an array of ints of odd length, return a new array length 3 containing the elements from the middle of the array. The array length will be at least 3.

midThree({1, 2, 3, 4, 5}) → {2, 3, 4}
midThree({8, 6, 7, 5, 3, 0, 9}) → {7, 5, 3}
midThree({1, 2, 3}) → {1, 2, 3}


Spoiler:
Code:
public int[] midThree(int[] nums) {
        int t[] = new int[3];
        if (nums.length > 3) {
            t[0] = nums[nums.length / 2 - 1];
            t[1] = nums[nums.length / 2];
            t[2] = nums[nums.length / 2 + 1];
            return t;
        }
        return nums;
    }


35.Given an array of ints of odd length, look at the first, last, and middle values in the array and return the largest. The array length will be a least 1.

maxTriple({1, 2, 3}) → 3
maxTriple({1, 5, 3}) → 5
maxTriple({5, 2, 3}) → 5


Spoiler:
Code:
public int maxTriple(int[] nums) {
        int temp = 0;

        if (nums.length == 2) {
            if (nums[0] >= nums[1]) {
                return nums[0];
            } else {
                return nums[1];
            }
        }
        if (nums.length >= 3) {
            if ((nums[0] >= nums[nums.length / 2]) && (nums[0] >= nums[nums.length - 1])) {
                return nums[0];
            }
            if ((nums[0] >= nums[nums.length / 2]) && (nums[0] <= nums[nums.length - 1])) {
                return nums[nums.length - 1];
            }
            if ((nums[0] <= nums[nums.length / 2]) && (nums[nums.length / 2] <= nums[nums.length - 1])) {
                return nums[nums.length - 1];
            }
            if ((nums[0] <= nums[nums.length / 2]) && (nums[nums.length / 2] >= nums[nums.length - 1])) {
                return nums[nums.length / 2];
            }
        }
        return 1;
    }


36.Given an int array of any length, return a new array of its first 2 elements. If the array is smaller than length 2, use whatever elements are present.

frontPiece({1, 2, 3}) → {1, 2}
frontPiece({1, 2}) → {1, 2}
frontPiece({1}) → {1}


Spoiler:
Code:
public int[] frontPiece(int[] nums) {
        if (nums.length >= 2) {
            int t1[] = new int[2];
            t1[0] = nums[0];
            t1[1] = nums[1];
            return t1;
        }
        return nums;
    }


37.We'll say that a 1 immediately followed by a 3 in an array is an "unlucky" 1. Return true if the given array contains an unlucky 1 in the first 2 or last 2 positions in the array.

unlucky1({1, 3, 4, 5}) → true
unlucky1({2, 1, 3, 4, 5}) → true
unlucky1({1, 1, 1}) → false


Spoiler:
Code:
public boolean unlucky1(int[] nums) {
        if (nums.length >= 2) {
            if ((nums[0] == 1) && (nums[1] == 3)) {
                return true;
            }
            if ((nums[nums.length - 2] == 1) && (nums[nums.length - 1] == 3)) {
                return true;
            }
        }

        if (nums.length >= 3) {
            if ((nums[1] == 1) && (nums[2] == 3)) {
                return true;
            }
        }
        return false;

    }


38.Given 2 int arrays, a and b, return a new array length 2 containing, as much as will fit, the elements from a followed by the elements from b. The arrays may be any length, including 0, but there will be 2 or more elements available between the 2 arrays.

make2({4, 5}, {1, 2, 3}) → {4, 5}
make2({4}, {1, 2, 3}) → {4, 1}
make2({}, {1, 2}) → {1, 2}


Spoiler:
Code:
public int[] make2(int[] a, int[] b) {
        int t[] = new int[2];
        if (a.length >= 2) {
            t[0] = a[0];
            t[1] = a[1];
            return t;
        }
        if (a.length == 1) {
            t[0] = a[0];
            t[1] = b[0];
            return t;
        }
        if ((a.length == 0) && (b.length >= 2)) {
            t[0] = b[0];
            t[1] = b[1];
            return t;
        }
        return t;

    }


39.Given 2 int arrays, a and b, of any length, return a new array with the first element of each array. If either array is length 0, ignore that array.

front11({1, 2, 3}, {7, 9, 8}) → {1, 7}
front11({1}, {2}) → {1, 2}
front11({1, 7}, {}) → {1}


Spoiler:
Code:
public int[] front11(int[] a, int[] b) {
        int t[] = new int[2];
        if ((a.length >= 1) && (b.length >= 1)) {
            t[0] = a[0];
            t[1] = b[0];
            return t;
        }
        if ((a.length >= 1) && (b.length < 1)) {
            int t1[] = new int[1];
            t1[0] = a[0];

            return t1;
        }
        if ((b.length >= 1) && (a.length < 1)) {
            int t1[] = new int[1];
            t1[0] = b[0];

            return t1;
        }

        return a;
    }
(This post was last modified: 09-19-2014, 10:40 AM by LordPankake.)
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: java basics exercises with solution !! #3
Nice ! Thank you guys, Today I hae startedtolearn java Biggrin

Reply

RE: java basics exercises with solution !! #4
I aced my Java class. We used Introduction to Java Programming by Liang for our text. If you like, I still have several sources that were homework, and my final projects.

Reply

RE: java basics exercises with solution !! #5
(07-15-2014, 05:07 PM)Nick Saban Wrote: I aced my Java class. We used Introduction to Java Programming by Liang for our text. If you like, I still have several sources that were homework, and my final projects.

yes feel free to share them on this thread with the solution !! ty Smile

i'll be sharing more soon !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: java basics exercises with solution !! #6
Thank you for sharing these exercises with the community.

One recommendation though, use "spoiler" to hide the answers...

Peace
[Image: wvBFmA5.png]

Reply

RE: java basics exercises with solution !! #7
Trying to figure out a way to share them all with you. Dropbox diabled my links for some reason, so I can't create a public link.

Reply

RE: java basics exercises with solution !! #8
(07-15-2014, 09:55 PM)Ligeti Wrote: Thank you for sharing these exercises with the community.

One recommendation though, use "spoiler" to hide the answers...

Peace

good idea thank you i'll edit them as soon as i add more exercises !!

(07-15-2014, 10:42 PM)Nick Saban Wrote: Trying to figure out a way to share them all with you. Dropbox diabled my links for some reason, so I can't create a public link.

try to upload them somewhere else !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: java basics exercises with solution !! #9
I would say to continue this try with basic exercises. It's intersting for the members who want to learn Java.:Thumbs-Up:

My relationship with Java, is not good because i have not occupied with her yet. But, i don't think i will spend time to this language in the future.

================================================================================================================
@"Ligeti",
yeah.
The suggestion of spoilers was nice. :Thumbs-Up:
[Image: T4OUWZ1.png]


Reply

RE: java basics exercises with solution !! #10
thread updated 3 new exercises added !!
[Image: blackeagle_zps6ad86521.gif]

Reply







Users browsing this thread: 1 Guest(s)