w3resource

Java Exercises: Print the values of x, y where a, b, c, d, e and f are specified

Java Basic: Exercise-214 with Solution

Write a Java program which solve the equation:
ax+by=c
dx+ey=f
Print the values of x, y where a, b, c, d, e and f are given.

Input:

a,b,c,d,e,f separated by a single space.
(-1,000 ≤ a,b,c,d,e,f ≤ 1,000)

Sample Solution:

Java Code:

import java.math.BigDecimal;
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayDeque<Double> x = new ArrayDeque<Double>();
        ArrayDeque<Double> y = new ArrayDeque<Double>();
        System.out.println("Input the value of a, b, c, d, e, f:");
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            int d = sc.nextInt();
            int e = sc.nextInt();
            int f = sc.nextInt();             
            double t = (double)(d*c-a*f)/(d*b-a*e);
            double s = (double)(c-t*b)/a;
            x.push(s);
            y.push(t);       
        int num = x.size();
        for(int i=0;i<num;i++){
            BigDecimal bdx = new BigDecimal(x.pollLast());
            BigDecimal bdy = new BigDecimal(y.pollLast()); 
            BigDecimal ansx = bdx.setScale(4, BigDecimal.ROUND_HALF_UP);
            BigDecimal ansy = bdy.setScale(4, BigDecimal.ROUND_HALF_UP);             
            System.out.printf("%.3f", ansx.doubleValue());
            System.out.print(" ");
            System.out.printf("%.3f", ansy.doubleValue());
            System.out.println();
        }         
    }
}

Sample Output:

Input the value of a, b, c, d, e, f:
 5 6 8 9 7 4
-1.684 2.737

Flowchart:

Flowchart: Java exercises: Print the values of x, y where a, b, c, d, e and f are specified.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to check whether three given lengths (integers) of three sides form a right triangle. Print "Yes" if the given sides form a right triangle otherwise print "No".
Next: Write a Java program to compute the amount of the debt in n months. The borrowing amount is $100,000 and the loan adds 4% interest of the debt and rounds it to the nearest 1,000 above month by month.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Java: Tips of the Day

Java: Anagrams

Generates all anagrams of a string.

public static List<String> anagrams(String input) {
    if (input.length() <= 2) {
        return input.length() == 2
                ? Arrays.asList(input, input.substring(1) + input.substring(0, 1))
                : Collections.singletonList(input);
    }
    return IntStream.range(0, input.length())
            .mapToObj(i -> new SimpleEntry<>(i, input.substring(i, i + 1)))
            .flatMap(entry ->
                    anagrams(input.substring(0, entry.getKey()) + input.substring(entry.getKey() + 1))
                            .stream()
                            .map(s -> entry.getValue() + s))
            .collect(Collectors.toList());
}

Ref: https://bit.ly/3rvAdAK