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...
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you...
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you familiar with some of the most used JAVA syntax, as well as constructors objects, objects calling other objects, class and instance attributes and methods. You will create a small program consisting of Musician and Song classes, then you will have Musicians perform the Songs. Included is a file “Assignment1Tester.java”. Once you have done the assignment and followed all instructions, you should be able to...
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...
The goal of Java program(s) implemented as part of this assignment is: 1. To use for,...
The goal of Java program(s) implemented as part of this assignment is: 1. To use for, while, do-while loops to solve the given problem(s) 2. Write custom method(s) Note: Be sure to document your code thoroughly. In other words, you must comment each pertinent line of code to briefly state what it is intending to do. Requirement-1 In the Requirement-1 you will create a program named ‘ComputeCompoundInterest, that will compute compound interest using the formula as follows: amount = principal...
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...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT