Question

In: Computer Science

The goal of this assignment is to write five short Java/Pyhton programs to gain practice with...

The goal of this assignment is to write five short Java/Pyhton programs to gain practice with expressions, loops and conditionals.

  1. Boolean and integer variables 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

  1. Type conversion. There are a number of different formats for representing color.  RGB format specifies the level of red (R), green (G), and blue (B) on an integer scale from 0 to 255:

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).

Solutions

Expert Solution

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
}

}
}


Related Solutions

Goal: Write a program that provides practice in class development and implementation of programs in multiple...
Goal: Write a program that provides practice in class development and implementation of programs in multiple separate files. Make sure you thoroughly read and understand the directions before you begin PART 1: Write a program that displays a temperature conversion chart. There should be 6 functions defined as part of a class. • print_introduction_message • get_conversion_table_specifications • print_message_echoing_input • fahrenheit_to_celsius • fahrenheit_to_kelvin • print_table A portion of the code is defined below but not using classes. Add the 4 missing...
Write 2 short Java programs based on the description below. 1) Write a public Java class...
Write 2 short Java programs based on the description below. 1) Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”} 2) Write a public Java...
Project Assignment The purpose of this assignment is for you to gain practice in applying the...
Project Assignment The purpose of this assignment is for you to gain practice in applying the concepts and techniques we learned in class. In order to complete the assignment, please do the following: 1. find or create a data set containing values of at least one interval or ratio variable for at least one-hundred cases (n >= 100); 1 2. provide basic descriptive statistics to summarize the central tendency and variability of the data; 3. provide at least one table...
The goal of Java program implemented as part of this assignment is to build an Inventory...
The goal of Java program implemented as part of this assignment is to build an Inventory Management System, from here on referred to as IMS, for a bookstore. You will need to do the following tasks: Requirement-1 – Create 3 different Arrays (15 pts) (a) Create an array named items that will hold the following item names (strings) - Pen - Pencil - Eraser - Marker - Notepad (b) Create a second array named quantity, that will hold corresponding quantities...
Assignment Overview This assignment will give you practice with interactive programs and if/else statements. Part 1:...
Assignment Overview This assignment will give you practice with interactive programs and if/else statements. Part 1: User name Generator Write a program that prompts for and reads the user’s first and last name (separately). Then print a string composed of the first letter of the user’s first name, followed by the first five characters of the user’s last name, followed by a random number in the range 10 to 99. Assume that the last name is at least five letters...
The goal of this project is to practice (Write a C Program) with a function that...
The goal of this project is to practice (Write a C Program) with a function that one of its parameter is a function.The prototype of this function is: void func ( float (*f)(float*, int), float* a, int length); This means the function: func has three parameters: float (*f)(float*, int): This parameter itself is a function: f that has two parameters and returns a floating-point number. In the body of the function: func we call the function: f with its arguments...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars
PLEASE WRITE IN C++ only . The objective of this assignment is to gain an understanding...
PLEASE WRITE IN C++ only . The objective of this assignment is to gain an understanding of the lexical analysis phase of a compiler and the process of constructing a symbol table. Problem: The first phase of compilation is called scanning or lexical analysis. This phase interprets the input program as a sequence of characters and produces a sequence of tokens, which will be used by the parser. Write a program (in C, C++, C#, Java, or Python) that implements...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
This programming assignment involves learning about some of the common exceptions that occur in Java programs.
This programming assignment involves learning about some of the common exceptions that occur in Java programs. Consider the following exception types: NullPointerException ArrayIndexOutOfBoundsException ClassCastException IllegalArgumentException Research what each exception type means and the conditions under which each occurs (i.e., is thrown). Write programs that demonstrate each type of exception being thrown (one program per exception) and provide a screen capture of the output. You should write your code so that each exception type is forced to occur. Name your programs...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT