Write a program called P4.c that creates an original Square Matrix containing in
ID: 3565734 • Letter: W
Question
Write a program called P4.c that creates an original Square Matrix containing integers and then creates a new
one with the values of the original reversed. The program should prompt the user for the size of the Square
Matrix.
Program Requirements/Specifications:
Define two structures to represent a Square Matrix: Vector(an array of integers) and Matrix(an array of
vectors).
Create a pointer to a structure of type Vector; dynamically allocate memory for the Vectors(matrix rows) and
populate the Vectors(array of integers) with integers, in ascending order, starting at one.
Create a structure of type Matrix and populate it with the pointer to a structure of type vector created above.
Create two Functions: Display() which will display the contents of a Matrix and Reverse() which will return a
new Matrix with the values reversed.
Example run:
> Enter size of Square Matrix(between 3 and 5): 4
Original matrix
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
New/reversed matrix
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
_____________
Explanation / Answer
In Java, decisions to do or not to do something can be made
by using the statement
if (<test expression>).
The <test expression> must be an expression that can be evaluated as either
true or false.
If it evaluates to true, a succeeding statement or group of statements
enclosed in
{ . . . },
called a block statement will be executed.
R1.1 How many * will be printed when the following code is executed
with n = 7 ?
...
if (n > 10) System.out.print("*****");
if (n > 7) System.out.print("****");
if (n > 4) System.out.print("***");
if (n > 1) System.out.print("**");
System.out.println("*");
===========================================
The outcome would be:
***
*
===========================================
----------------------------------------------------------
R2. Relations and Relational Operators
----------------------------------------------------------
The relational operators in Java are
==
!=
<
>
<= and
>=
R2.1 Formulate the following conditions in Java:
x is positive
x is zero or negative
x is at least 10
x is less than 10
x and y are both zero
x is even
===========================================
x > 0 //x greater than 0, x is positive
x <= 0 //x is 0, or less than zero
x == 10 //x is 10
x < 10 //x is less than 10
x, y == 0 //x and y are 0
x%2 = 0 //x is even
===========================================
----------------------------------------------------------
P1. Input validation
----------------------------------------------------------
In assignment one of this course you are asked to input
the starting velocity of a projectile being fired vertically
into the air.
P1.1 Write a code fragment that will validate the input value.
(hint: how slow or fast is it possible to shoot a projectile).
===========================================
double x; //x is the starting velocity of projectile
if (x > 0)
System.out.println("The starting velocity you inputted is" + x ".");
else
System.out.println(x + " is an invalid velocity. Please try again.");
===========================================
P1.2 What other things might you check for in terms of
validating this input?
===========================================
Check to see that they dont type in letters in place of numbers, such as
"l" and "1".
===========================================
----------------------------------------------------------
P2. The if/else statement
----------------------------------------------------------
You can use the if/else format to explicitly specify an
alternative action.
if (test_expression)
/* do something ... */
else
/* do something different ... */
P2.1 A wholesaler advertises a volume discount on blank CD ROMs of
10 cents/disk for each thousand purchased above 10000.
(The regular price is $950.00 per 1000, or 95 cents per disk).
Write a program that receives the number of blank CD ROMs in
an order, then calculates and displays the total cost.
===========================================
public class CdStore
{
public static void main(String[] args)
{
final double REG_PRICE = 0.95; //original price
final double AD_PRICE = 0.85; //discount price
ConsoleReader console = new ConsoleReader(System.in);
System.out.println("How many Cd's are you purchasing?");
int cd_amnt = console.readInt(); //gets amount of cd's purchasing
if (cd_amnt>10000)
{
double Total = (10000*REG_PRICE) + ((cd_amnt-10000)*(AD_PRICE));
System.out.println("Your total is $" + Total + ".");
}
else
{
double Total = cd_amnt * REG_PRICE;
System.out.println("Your total is $" + Total + ".");
}
}
}
===========================================
P2.2 According to your program, how much will it cost to buy
a) 2000 disks?
b) 15000 disks?
c) 180000 disks?
===========================================
a) $1900
b) $13750.0
c) $154000.0
===========================================
----------------------------------------------------------
P3. Multiple alternatives
----------------------------------------------------------
The if/else decision in the preceding example can be extended
to select from more than twp possible outcomes.
The if ... else if ... else
syntax is used to select exactly one of several possible
actions.
if (test expression 1)
/* do something ... */
else if (test expression 2)
/* do something different ... */
else
/* do something generic ... */
P3.1 Write a program to compute the cost of a purchase of blank CD ROMs
as above, but this time, have there be no discount on the first 10000,
5 cents/disk on the second 10,000, 10 cents/disk over 20,000 and 20
cents/disk on any over 50,000.
===========================================
public class CdStore
{
public static void main(String[] args)
{
final double REG_PRICE = 0.95; //regular price
final double DISCOUNT1 = 0.05; //discount on second 10000 cd purchase
final double DISCOUNT2 = 0.10; //discount on on purchase over 20000
final double DISCOUNT3 = 0.20; //discount on purchase over 50000
ConsoleReader console = new ConsoleReader(System.in);
System.out.println("How many Cd's are you purchasing?");
int cd_amnt = console.readInt();
if (cd_amnt>1 && cd_amnt<=10000)
{
Total = cd_amnt * REG_PRICE;
}
else if (cd_amnt>10000 && cd_amnt<=20000)
{
Total = (10000 * REG_PRICE) + ((cd_amnt-10000) * (REG_PRICE - DISCOUNT1));
}
else if (cd_amnt>20000 && cd_amnt<=50000)
{
Total = (10000 * REGPRICE) + (10000 * (REG_PRICE-DISCOUNT1)) + ((cd_amnt-20000) * (REG_PRICE - DISCOUNT2));
}
else if (cd_amnt>50000)
{
Total=(10000 * 0.95) + (10000 * (REG_PRICE - DISCOUNT1)) + (30000 * (REG_PRICE-DISCOUNT2)) + (((cd_amnt-50000) * (REG_PRICE - discount3)));
}
System.out.println("Your total is $" + Total + ".");
}
}
===========================================
----------------------------------------------------------
P4. Nested Branches
----------------------------------------------------------
If there are multiple conditions, it can happen that a
conditionally executed block contains further decisions.
Here is an example.
P4.1 Extend the following code to test whether two circles -
each having a fixed center point and a user-defined radius -
are
disjoint (not overlapping or contained),
overlapping (perimeters cross) or
mutually contained (One within another).
public class CircleOverlap
{
public static void main(String[] args)
{
ConsoleReader console = new ConsoleReader(System.in);
System.out.println("Radius 1:");
double radius1 = console.readDouble();
double xcenter1 = 0;
double ycenter1 = 0;
System.out.println("Radius 2:");
double radius2 = console.readDouble();
double xcenter2 = 40;
double ycenter2 = 0;
/* Your work goes here */
}
}
===========================================
public class CircleOverlap
{
public static void main(String[] args)
{
ConsoleReader console = new ConsoleReader(System.in);
System.out.println("Radius 1:");
double radius1 = console.readDouble();
double xcenter1 = 0;
double ycenter1 = 0;
System.out.println("Radius 2:");
double radius2 = console.readDouble();
double xcenter2 = 40;
double ycenter2 = 0;
if ((radius1 + radius2) < 40)
System.out.println("These circles are disjoint.");
else
if ((radius1 >= (40 + radius2)) || (radius2 >= (40 + radius1)))
System.out.println("These circles are within each other.");
else
System.out.println("These circles are touching.");
}
}
===========================================
----------------------------------------------------------
R3. Logical Operations
----------------------------------------------------------
Java has three logical operations,
&&,
|| and
!.
R3.1 Using these operations, express the following:
x and y are both positive or neither of them is positive.
===========================================
(x & y) > 0 && (x & y) <= 0
===========================================
R3.2 Formulate the following conditions on the date that is given by the
variables day and month
a) The date is in the second quarter of the year.
b) The date is the last day of the month. (Assume February always has 28 days).
c) The date is before April 15.
===========================================
a) if (month == April) || (month == May) || (month==June)
{ if (day <=31)
System.out.println("The date is in the second quarter of the year");
}
b) if (month == January) || (month == March) || (month == May) || (month == July) || (month == August) || (month == October) || (month == December)
{
day = 31;
}
else if (month == February)
{
day = 28;
}
else
{
day = 30;
}
System.out.println(day);
c)
int x = 14 //14th day in april
if (month == January) || (month == March) || (month == May) || (month == July) || (month == August) || (month == October) || (month == December)
{
day = 31;
}
else if (month == February)
{
day = 28;
}
else
{
day = 30
}
System.out.println(day-x);
===========================================
----------------------------------------------------------
P5. Logical Operations
----------------------------------------------------------
The following program determines if a particular package is
elligible for Unidentified Delivery Service's special Northwest
Urban Rate Discount.
P5.1 Simplify the nested branches by using logical operations
&&, ||, ! wherever possible.
public class UPS
{
public static void main(String[] args)
{
boolean first;
boolean second;
ConsoleReader console = new ConsoleReader(System.in);
System.out.println("From Address:");
String fromAddress = console.readLine();
System.out.println("From City:");
String fromCity = console.readLine();
System.out.println("From State/Province:");
String fromState = console.readLine();
System.out.println("To Address:");
String toAddress = console.readLine();
System.out.println("To City:");
String toCity = console.readLine();
System.out.println("To State/Province:");
String toState = console.readLine();
if (fromState.equals("OR"))
{
if (fromAddress.substring(0,11).equals("Rural Route"))
first = false;
else
first = true;
}
else
if(fromState.equals("WA"))
{
if (fromAddress.substring(0,11).equals("Rural Route"))
first = false;
else
first = true;
}
else if (fromState.equals("BC"))
{
if (fromAddress.substring(0,11).equals("Rural Route"))
first = false;
else
first = true;
}
else
first = false;
if (toState.equals("OR"))
{
if (toAddress.substring(0,11).equals("Rural Route"))
second = false;
else
second = true;
}
else if (toState.equals("WA"))
{
if (toAddress.substring(0,11).equals("Rural Route"))
second = false;
else
second = true;
}
else if (toState.equals("BC"))
{
if (toAddress.substring(0,11).equals("Rural Route"))
second = false;
else
second = true;
}
else
second = false;
if (first && second)
System.out.println
("Package eligible for Northwest Urban Rate Discount!");
else
System.out.println
("Package eligible for Standard Rate");
}
}
===========================================
public class UPS
{
public static void main(String[] args)
{
boolean first;
boolean second;
ConsoleReader console = new ConsoleReader(System.in);
System.out.println("From Address:");
String fromAddress = console.readLine();
System.out.println("From City:");
String fromCity = console.readLine();
System.out.println("From State/Province:");
String fromState = console.readLine();
System.out.println("To Address:");
String toAddress = console.readLine();
System.out.println("To City:");
String toCity = console.readLine();
System.out.println("To State/Province:");
String toState = console.readLine();
if (((fromState.equals("OR")) || (fromState.equals("WA")) || (fromState.equals("BC"))) && (!(fromAddress.substring(0,11).equals("Rural Route"))))
first = true;
else
first = false;
if (((toState.equals("OR")) || (toState.equals("WA")) || (toState.equals("BC"))) && (!(toAddress.substring(0,11).equals("Rural Route"))))
second = true;
else
second = false;
if (first && second)
System.out.println
("Package eligible for Northwest Urban Rate Discount!");
else
System.out.println
("Package eligible for Standard Rate");
}
}
===========================================
----------------------------------------------------------
R4. Using Boolean variables
----------------------------------------------------------
R4.1 According to the following program, what color is the
resulting mixture under the following inputs?
Y N Y
Y Y N
N N N
public class ColorMixer
{
public static void main(String[] args)
{
String mixture = "";
boolean red = false;
boolean green = false;
boolean blue = false;
String input;
ConsoleReader console = new ConsoleReader(System.in);
System.out.println("Include red in mixture? (Y/N)");
input = console.readLine();
if (input.toUpperCase().equals("Y"))
red = true;
System.out.println("Include green in mixture? (Y/N)");
input = console.readLine();
if (input.toUpperCase().equals("Y"))
green = true;
System.out.println("Include blue in mixture? (Y/N)");
input = console.readLine();
if (input.toUpperCase().equals("Y"))
blue = true;
if (!blue && !green)
mixture = "RED";
else if (!red && !blue)
mixture = "GREEN";
else if (!red && !green)
mixture = "BLUE";
else if (red)
{ if (green || blue)
{ if (green && blue)
mixture = "BLACK";
else if (green)
mixture = "YELLOW";
else
mixture = "PURPLE";
}
}
else
{
if (blue && green)
mixture = "CYAN";
else
mixture = "WHITE";
}
System.out.println("Your mixture is " + mixture);
}
}
===========================================
The color output is as shown below:-
a)Your mixture is PURPLE
b) Your mixture is YELLOW
c) Your mixture is RED
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.