Welcome to Dream.In.Code
Become a Java Expert!

Join 150,196 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,023 people online right now. Registration is fast and FREE... Join Now!




Correct?

 
Reply to this topicStart new topic

Correct?

mse12
24 Sep, 2008 - 03:44 PM
Post #1

D.I.C Head
**

Joined: 21 Sep, 2008
Posts: 55

These are the 3 questions and I am a beginner at Java so I'm not positive if I'm answering the questions correctly/doing what the question s asking...

Thank you in advance for any help!

(1)- Write a program that uses a method to calculate the average of three double numbers. The
method should receive three parameters and return the average of them. Call the method from your
main() method using any three numbers you wish, and print the result. There is no user input for this
problem.

CODE
// Author: Mark Einsiedel (mse12)

public class Lab4_Problem1 {
  public static void main(String[] args) {
    // Write a program that uses a method to calculate the average of three double numbers. The
    // method should receive three parameters and return the average of them. Call the method from your
    // main() method using any three numbers you wish, and print the result. There is no user input for this
    // problem.
    
    double number1 = 1.55;
    double number2 = 2.55;
    double number3 = 3.55;
    double avg = average(number1, number2, number3);
    
    System.out.println("The average of the 3 numbers is " + avg);
    
    }
  public static double average(double number1, double number2, double number3) {
    double result;
    
    result = (number1 + number2 + number3) / 3;
    
    return result;
   }
}







(2) - Write your own println() method that receives one double parameter and does not
return anything. Call this new method from inside your main() method to print out a real-valued
number (such as 3.14). There is no user input for this problem. Note that you will need to use
System.out.println() inside the method. However you can now print anything from the
main() method with a little less typing.

CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication50;

/**
*
* @author Mark Einsiedel (mse12)
*/ //Lab 4 Problem 2
public class Lab4_Problem2 {

    /**
     * @param args the command line arguments
     */
    
    public static void main (String [] args) {
    
    }
    public static double nPrintln(String message, double x) {
            // Write your own println() method that receives one double parameter and does not
            // return anything. Call this new method from inside your main() method to print out a real-valued
            // number (such as 3.14). There is no user input for this problem. Note that you will need to use
            // System.out.println() inside the method. However you can now print anything from the
            // main() method with a little less typing.
    
    double number = 3.14;
    
    double result = number;
    
    System.out.println("The number is " + number);
    
    return result;
    }
        
}





(3) - We have been using methods from Java libraries. As two examples, consider the methods
System.out.println()and Math.random(). The name of the library is always required,
followed by the dot separator “.”, followed by the actual method name. For this problem, you will use
the RandomCharacter library created by the author of your textbook and shown in Listing 5.8 (p 158) of
your textbook. Perform the following steps:
a. To use a custom library like this, you must compile it. To save time, the .java file for this library
is posted on our course website. Download the file and compile it.
b. In a separate .java file, create a program that uses the RandomCharacter library to print the
following:
i. One random lowercase letter.
ii. One random uppercase letter.
iii. One random numerical digit.
c. Use the DrJava debugger to step through your program and record each line number that the
debugger stops at, particularly when it jumps to the RandomCharacter.java file. Include the line
numbers in a comment somewhere in your program for your TA to grade this part. Remember
that you can display line numbers in DrJava by going to Edit/Preferences/DisplayOptions and
checking “Show All Line Numbers”. Below is an example (but it is incorrect!):
// Lab4.java: 3, 4, 5
// RandomCharacter.java: 4, 5, 6, 7, 8
// Lab4.java: 6, 7
// RandomCharacter.java: 4, 5, 6, 7, 8
Hints: Look at the methods in the code to see what parameters, if any, are needed when calling each
method. Look at the highlighted code on line 9 of the example TestRandomCharacter.java (Listing 5.9,
p 158).

CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication51;

/**
*
* @author Mark Einsiedel (mse12)
*/
public class Lab4_Problem3 {

    /**
     * @param args the command line arguments
     */
        // Create a program that uses the RandomCharacter library to print the
        // following:
        // i. One random lowercase letter.
        // ii. One random uppercase letter.
        // iii. One random numerical digit.
    
    public static char getRandomCharacter(char ch1, char ch2) {
        return (char)(ch1 + Math.random() * (ch2 - ch1 +1));
    }
    
    /** Generate one random lowercase letter (part i) */
    public static char getRandomLowerCaseLetter() {
        return getRandomCharacter('a', 'z');
    }  
    
    /** Generate one random upercase letter (part ii) */
    public static char getRandomUpperCaseLetter() {
        return getRandomCharacter('A', 'Z');
    }
    
     /** Generate one random numerical digit (part iii) */
    public static char getRandomDigitCharacter() {
        return getRandomCharacter('0', '9');
    }
}





User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: Correct?
24 Sep, 2008 - 04:01 PM
Post #2

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
Problem 1 looks good

Problem 2 not so good i dont think

Write your own println() method that receives one double parameter and does not
return anything


#1 your method is called nPrintln method and it says to call it println
#2 you are having the method return a double and it says it should not return anything
#3 you are passing in a String and double, when it says to only pass in a double
CODE

//incorrect
public static double nPrintln(String message, double x) {

//correct
public static void println(double x) {


Call this new method from inside your main() method to print out a real-valued
number (such as 3.14). There is no user input for this problem. Note that you will need to use
System.out.println() inside the method. However you can now print anything from the
main() method with a little less typing.

#4 currently your main method is blank, it says to call the created println method from the main method, so call the println method and pass in the correct arguments


This post has been edited by Mach1Guy: 24 Sep, 2008 - 04:09 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Correct?
24 Sep, 2008 - 05:34 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
First assignment says that you should received the 3 double as argument on your command line. So:

CODE

public static void main(String[] arg) {
    double double1 = Double.parseDouble(arg[0]);
    double double2 = Double.parseDouble(arg[1]);
    double double3 = Double.parseDouble(arg[2]);
....
     the rest is correct

}

User is offlineProfile CardPM
+Quote Post

mse12
RE: Correct?
24 Sep, 2008 - 05:58 PM
Post #4

D.I.C Head
**

Joined: 21 Sep, 2008
Posts: 55

ok i made some revisions please see how i did-thank you

prob 1:


CODE
// Author: Mark Einsiedel (mse12)

public class Lab4_Problem1 {
  public static void main(String[] args) {
    // Write a program that uses a method to calculate the average of three double numbers. The
    // method should receive three parameters and return the average of them. Call the method from your
    // main() method using any three numbers you wish, and print the result. There is no user input for this
    // problem.
    
    double double1 = Double.parseDouble(args[0]);
    double double2 = Double.parseDouble(args[1]);
    double double3 = Double.parseDouble(args[2]);
    double avg = average(double1, double2, double3);
    
    System.out.println("The average of the 3 numbers is " + avg);
    
    }
  public static double average(double double1, double double2, double double3) {
    double result;
    
    result = (double1 + double2 + double3) / 3;
    
    return result;
   }
}






prob 2

CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication50;

/**
*
* @author Mark Einsiedel (mse12)
*/ //Lab 4 Problem 2
public class Lab4_Problem2 {

    /**
     * @param args the command line arguments
     */
    
    public static void main (String [] args) {
    
    }
    public static double println(double x) {
            // Write your own println() method that receives one double parameter and does not
            // return anything. Call this new method from inside your main() method to print out a real-valued
            // number (such as 3.14). There is no user input for this problem. Note that you will need to use
            // System.out.println() inside the method. However you can now print anything from the
            // main() method with a little less typing.
    
    double number = 3.14;
    
    double result = number;
    
    System.out.println("The number is " + number);
    
    return result;
    }
        
}
        




prob 3
CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication51;

/**
*
* @author Mark Einsiedel (mse12)
*/
public class Lab4_Problem3 {

    /**
     * @param args the command line arguments
     */
        // Create a program that uses the RandomCharacter library to print the
        // following:
        // i. One random lowercase letter.
        // ii. One random uppercase letter.
        // iii. One random numerical digit.
    
    public static char getRandomCharacter(char ch1, char ch2) {
        return (char)(ch1 + Math.random() * (ch2 - ch1 +1));
    }
    
    /** Generate one random lowercase letter (part i) */
    public static char getRandomLowerCaseLetter() {
        return getRandomCharacter('a', 'z');
    }  
    
    /** Generate one random upercase letter (part ii) */
    public static char getRandomUpperCaseLetter() {
        return getRandomCharacter('A', 'Z');
    }
    
     /** Generate one random numerical digit (part iii) */
    public static char getRandomDigitCharacter() {
        return getRandomCharacter('0', '9');
    }
}



User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: Correct?
24 Sep, 2008 - 06:07 PM
Post #5

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
you are still having problems with problem 2. the way you have it written right now it is going to return a double, but it says
// Write your own println() method that receives one double parameter and does not return anything.

the means you need to change the following word double to void:
public static double println(double x) {
public static void println(double x) {

delete the line that says: return result;
also, you still need to make the call to println from your main method

This post has been edited by Mach1Guy: 24 Sep, 2008 - 06:07 PM
User is offlineProfile CardPM
+Quote Post

mse12
RE: Correct?
24 Sep, 2008 - 06:10 PM
Post #6

D.I.C Head
**

Joined: 21 Sep, 2008
Posts: 55

QUOTE(Mach1Guy @ 24 Sep, 2008 - 07:07 PM) *

also, you still need to make the call to println from your main method


what does that mean exactly?
User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: Correct?
24 Sep, 2008 - 06:14 PM
Post #7

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
CODE
    public static void main (String [] args) {
          println(3.14);
    }


OR

CODE
    public static void main (String [] args) {
        double number = 3.14;
        println(number);
    }


basically only the code in the main method is going to execute when the program starts, therefore you need to somehow invoke/call the other methods that you have created. above are 2 different ways you could do so (essentially just the method name and then pass in a double as the argument)

This post has been edited by Mach1Guy: 24 Sep, 2008 - 06:16 PM
User is offlineProfile CardPM
+Quote Post

mse12
RE: Correct?
24 Sep, 2008 - 06:17 PM
Post #8

D.I.C Head
**

Joined: 21 Sep, 2008
Posts: 55

oh easy enough and great explanation.

thank you very much.

#1 & #3 look good then?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:39AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month