Question

#1. Operators and values, expressions Please correct the following code to make it print the given...

#1. Operators and values, expressions

Please correct the following code to make it print the given expressions and their answers well (3pts).

public class DataTypes {

   public static void main(String[] args) {

      System.out.println("5*8/6 = " + 5*8/6);

      System.out.println("(2*6)+(4*4)+10 = " + (2*6)+(4*4)+10);

      System.out.println("6 / 2 + 7 / 3 = " + 6 / 2 + 7 / 3);

      System.out.println("6 * 7 % 4 = " + 6 * 7 % 4 );

      System.out.println("22 + 4 * 2 = " + 22 + 4 * 2);

   }

}

#2. String Concatenation

Please practice the following string expressions manually and compare your answers with the computer generated strings. Submit the code with answers embedded in the code as comments (3pts).

public class Evaluation {

   public static void main(String[] args) {

      System.out.println("Hello, world!");

      System.out.println("Hello " + "World!");

      System.out.println("Summer " + 2017);

      System.out.println("Interstate " + 9 + 5);

      System.out.println(20 + 20 + " Class");

      System.out.println("Class " + 20 + 20);

   }

}

#3. for-loop and a method declaration

Please write a program which invokes a method, drawBox(6). The drawBox(int num) method prints the following figure: (3pts)

*****1

****22

***333

**4444

*55555

666666

#4. for-loop and a method declaration

Please write a method which recevies an integer N as its parameter and prints N different number of odd numbers increasing order starting from 1 like the following sequence of numbers. The method can be invoked from the main() method in the previous question (#3) (3pts).

      The expected output: 1   3   5   7   9   11

The method is invoked like the following statement:

      printOddNumbers(6);   // This method is to print 6 increasing order of odd numbers starting from 1.

#5. Variables and a method declaration

Please write a method which returns the slope of the line between the given points: Point1 (x1, y1) and Point2 (x2, y2) (3pts).

Public static double slope (int x1, int y1, int x2, int y2) {

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi I have answered first 3 questions.

Please repost others in separate post

1.

public class DataTypes {

   public static void main(String[] args) {

       System.out.println("5*8/6 = " + (5*8/6));

       System.out.println("(2*6)+(4*4)+10 = " + ((2*6)+(4*4)+10));

       System.out.println("6 / 2 + 7 / 3 = " +( 6 / 2 + 7 / 3));

       System.out.println("6 * 7 % 4 = " + (6 * 7 % 4) );

       System.out.println("22 + 4 * 2 = " +( 22 + 4 * 2));

   }

}

/*

Output:

5*8/6 = 6

(2*6)+(4*4)+10 = 38

6 / 2 + 7 / 3 = 5

6 * 7 % 4 = 2

22 + 4 * 2 = 30

*/

2.

public class Evaluation {

   public static void main(String[] args) {

       System.out.println("Hello, world!"); // output : Hello, world!

       System.out.println("Hello " + "World!"); // output: Hello World!

       System.out.println("Summer " + 2017); // output Summer 2017

       System.out.println("Interstate " + 9 + 5); // output: Interstate 95

       System.out.println(20 + 20 + " Class"); // output: 40 Class

       System.out.println("Class " + 20 + 20); // output: Class 2020

   }

}

3)

public void drawBox(int num) {

      

       for(int i=1; i<=num; i++) {

          

           for(int j=1; j<=(num-i); j++) {

               System.out.print("*");

           }

           for(int k=1; k<=i; k++)

               System.out.print(i);

           System.out.println();

       }

   }

Add a comment
Know the answer?
Add Answer to:
#1. Operators and values, expressions Please correct the following code to make it print the given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, Could you please input validate this code so that the code prints an error message...

    Hello, Could you please input validate this code so that the code prints an error message if the user enters in floating point numbers or characters or ANYTHING but VALID ints? And then could you please post a picture of the output testing it to make sure it works? * Write a method called evenNumbers that accepts a Scanner * reading input from a file with a series of integers, and * report various statistics about the integers to the...

  • I need to make this code access the main method I suppose, but I don't know...

    I need to make this code access the main method I suppose, but I don't know how to make that happen... Help please! QUESTION: Write a method called switchEmUp that accepts two integer arrays as parameters and switches the contents of the arrays. Make sure that you have code which considers if the two arrays are different sizes. Instructor comment: This will work since you never go back to the main method, but if you did, it wouldn't print out...

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • Question 6 (1 point) What is the output of the following code snippet? Please ensure your...

    Question 6 (1 point) What is the output of the following code snippet? Please ensure your answe. spaced according to the program code output. public static void main(String[] args) int channel = assignChannel (2); System.out.println("Channel: " + channel); ) public static int assignChannel(int channel) return channel + 3; > A/

  • Please explain if the following code is actually correct. If the following code correct, please explain...

    Please explain if the following code is actually correct. If the following code correct, please explain why the code works and is also correct. Don’t use * Java’s Integer .toBinaryString(int) in this program./* According to the textbook and the instructor, I am not supposed to use arrays such as int binary[] = new int[25]; I guess this is the reason why this problem is starting to look kind of hard.   Chapter 5 Exercise 37: Java Programming * * (Decimal to...

  • in java Part 1 In this section, we relook at the main method by examining passing...

    in java Part 1 In this section, we relook at the main method by examining passing array as parameters. Often we include options/flags when running programs. On command line, for example, we may do “java MyProgram -a -v". Depending on options, the program may do things differently. For example, "a" may do all things while "-v" display messages verbosely. You can provide options in Eclipse instead of command line: "Run ... Run Configurations ... Arguments". Create a Java class (Main.java)....

  • Please Help *Urgent* 1 /* */ 2 public class LabFinal 3 { 4 public static void...

    Please Help *Urgent* 1 /* */ 2 public class LabFinal 3 { 4 public static void main(String[] args) { 5 String mesg = "Try out this String."; 6 char[] hold = mesg.toLowerCase().toCharArray(); 7 int p=0; 8 9 System.out.println(forwardSearch(hold,'s')); //output generated 11 10 System.out.println(forwardSearch(hold,'t')); //output generated 0 11 System.out.println(p=forwardSearch(hold,'t',2)); //output generated 6 12 System.out.println(p=forwardSearch(hold,'t',p+1)); //output generated 8 13 System.out.println(p=forwardSearch(hold,'z')); //output generated -1 14 System.out.println(backwardSearch(hold,'s')); //output generated 13 15 System.out.println(backwardSearch(hold,'t')); //output generated 14 16 System.out.println(p=backwardSearch(hold,'t',13)); //output generated 8 17 System.out.println(p=backwardSearch(hold,'t',p-1)); //output generated...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT