Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Multiple Java Programs That I need Help with

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Multiple Java Programs That I need Help with

    Question 1:

    What is the output of the following code?
    public class Question02b{
           public static void main(String[] args){ SuperClass superA = new
               SuperClass(); SuperClass superB = new SubClass(); SubClass
               sub = new SubClass(); System.out.println(superA.method());
               System.out.println(superB.method());
               System.out.println(sub.method());
               System.out.println(superB.method(1));
               System.out.println(superB.method(1.4));
    }}
       class SuperClass { public static String method(){
               return "SuperClass"; } public String method(double a){
        return "SuperClass"; } }
       class SubClass extends SuperClass{ public static String method(){
               return "SubClass";
    }
    public String method(int a){
        return
    "SubClass"; } }
    Question 3 is on page 4


    Question 2:
    What are the possible types of objects at runtime that veg in the method omnivore may be pointing
    to? And what would the output be?

    public class Question03 { void omnivore(Vegetable veg){ veg.eat(); } }
        abstract class Vegetable { protected int roots = 7; public void eat(){
            slice(); saute(); } public static void saute(){
            System.out.println("Sizzle!"); } public abstract void slice();
    }
        class Tuber extends Vegetable { Tuber(){ mash(); } public void slice(){
            System.out.println("Chop!"); mash();} public void mash(){
            System.out.println("Glop! " + roots); }
    }
        class Potato extends Tuber
            { Potato(){ roots = 59; }
            public void eat(){
    if(this.equals(new Potato())) System.out.println("Spud!"); else
        System.out.println("Spudless!");
                super.eat(); } public static void saute(){
            System.out.println("Crisps!"); } public void mash(){
            System.out.println("Creamy! " + roots); }
    }

    Question 3:
    There are two errors in this class, what are they and how can they be fixed?
    public class Question04c extends OtherClass{
    protected int answer(int i) throws DumbQuestionException { if(i < 0){ throw
        new DumbQuestionException(); } else return 6; } }
    class OtherClass{ public int answer(int i){ return i; } }
        class DumbQuestionException extends Exception{ }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Multiple Java Programs That I need Help with

    Do you have any specific question on your assignment?

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Multiple Java Programs That I need Help with

    Quote Originally Posted by jps View Post
    Do you have any specific question on your assignment?
    I did question 1
    But i still am not sure about Questions 2 and 3

    My answers are:

    Q2:
    It leads to roots, eat, slice, sauté, and mash
    Output:
    Sizzle!, Chop!, Glop! + 7, Spud!, Spudless!, Crisps, Creamy! + 59

    Q3:
    You are attempting to assign weaker privileges when it was public.

Similar Threads

  1. How java programs store in Heap and Stack.
    By rayan2004 in forum Object Oriented Programming
    Replies: 3
    Last Post: December 14th, 2012, 07:46 AM
  2. Replies: 1
    Last Post: April 26th, 2012, 10:06 AM
  3. programs of java
    By chinu in forum Java Servlet
    Replies: 2
    Last Post: July 26th, 2011, 12:08 PM
  4. How do I create these Java programs?
    By RYANSUPERGENIUS1123 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 22nd, 2011, 07:05 PM
  5. Java Loop and more Practical Programs
    By mparthiban in forum Loops & Control Statements
    Replies: 0
    Last Post: March 6th, 2010, 01:39 AM