In: Computer Science
The goal of this assignment is to write five short Java/Pyhton programs to gain practice with expressions, loops and conditionals.
Write a program Ordered.java that reads in three integer command-line arguments, x, y, and z. Define a boolean variable isOrdered whose value is true if the three values are either in strictly ascending order (x < y < z) or in strictly descending order (x > y > z), and false otherwise.
Print out the variable isOrdered using System.out.println(isOrdered).
% java Ordered 10 17 49
true
% java Ordered 49 17 10
true
% java Ordered 10 49 17
false
It is the primary format for LCD displays, digital cameras, and web pages. CMYK format specifies the level of cyan (C), magenta (M), yellow (Y), and black (K) on a real scale of 0.0 to 1.0:
It is the primary format for publishing books and magazines.
Write a program RGBtoCMYK.java that reads in three integers red, green, and blue from the command line and prints out equivalent CMYK parameters. The mathematical formulas for converting from RGB to an equivalent CMYK color are:
Hint. Math.max(x, y) returns the maximum of x and y.
% java RGBtoCMYK 75 0 130 // indigo
cyan = 0.423076923076923
magenta = 1.0
yellow = 0.0
black = 0.4901960784313726
If all three red, green, and blue values are 0, the resulting color is black (0 0 0 1).
Ordered Java Code:
import java.util.Scanner;
public class Ordered {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("enter three integers x, y and z");
int x=input.nextInt();
int y=input.nextInt();
int z=input.nextInt();
boolean isOrdered=true;
if( (x < y && y<z ) || (x>y && y>z)
)
{
isOrdered=true;
System.out.println(isOrdered);
}
else
{
isOrdered=false;
System.out.println(isOrdered);
}
}
}
Snapshot of output obtained:
Second output:
Third Output:
Explanation:
import java.util.Scanner; //package to get the import
tScanner
public class Ordered {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("enter three integers x, y and z"); //asking for
input from user
int x=input.nextInt(); //Reading first integer from Console and
storing in x
int y=input.nextInt(); //Reading second integer from Console and
storing in y
int z=input.nextInt(); //Reading third integer from Console and
storing in z
boolean isOrdered=true; //defining a variable isOrdered which is of
type boolean .
if( (x < y && y<z ) || (x>y && y>z) )
//conndition to check (x<y<z) or (x>y>z)
{
isOrdered=true; //if the condition is passed then isOrdered
variable is declared to true
System.out.println(isOrdered); //printing the result
}
else
{
isOrdered=false; //if the condition if not true then isOrdered
variable is declared to false
System.out.println(isOrdered); //printing the result
}
}
}