Question

Java Homework Problems: 4. • Examine AddImport.java. – Perform the following: – Replace the fully qualified...

Java Homework Problems:

4.

• Examine AddImport.java.

– Perform the following:

– Replace the fully qualified name to access the Jlabel

component with an import statement.

– To import classes from the util package, replace multiple

import statements with a single import statement.

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

import java.util.Calendar;

import java.util.Date;

public class AddImport {

    public static void main(String args[]) {

        javax.swing.JLabel label = new javax.swing.JLabel("hello");

    }

}

===========================================================

5.

Exercise 1

• Examine ShoppingCart.java.

– Perform the following:

– Use the indexOf method to get the index for the space character (" ") within custName. Assign it to spaceIdx.

– Use the substring method and spaceIdx to get the first name portion of custName. Assign it to firstName and print firstName.

public class ShoppingCart {

    public static void main (String[] args){

        String custName = "Steve Smith";

        String firstName;

        int spaceIdx;

       

        // Get the index of the space character (" ") in custName.

        // Use the substring method parse out the first name and print it.

    }

   

}

Exercise 2

Examine NameMaker.java.

• Perform the following:

– Declare String variables: firstName, middleName, lastName, and fullName

– Prompt users to enter their first, middle, and last names and read the names from the keyboard.

– Set and display the fullName as firstName+a blank char+middleName+a blank char+lastName.

public class NameMaker {

   

    public static void main(String args[])

    {

       

    }

   

}

Exercise 2

• Which do you think is preferable for this scenario?

• That is, the string concatenation operator or the concat() method?

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

AddImport.java

//To import classes from the util package, replace multiple
//import statements with a single import statement.
import java.util.*;
//import all swing package
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class AddImport {

public static void main(String args[]) {
   //JFrame
   JFrame f=new JFrame();
  
   //JLabel
   JLabel label = new JLabel("hello");
  
   //JPanel
   JPanel p = new JPanel();
  
   //add JLabel To Panel
   p.add(label);
  
   //add JPanel to JFrame
   f.add(p);
  
// set the size of frame
f.setSize(300, 300);
  
//To show JFrame setVisible(true)
f.setVisible(true);

}

}

Output

5. Exercise 1

ShoppingCart.java

public class ShoppingCart {

public static void main (String[] args){

   String custName = "Steve Smith";

String firstName;

int spaceIdx;

// Get the index of the space character (" ") in custName.
spaceIdx = custName.indexOf(" ");
System.out.println("Index of space character (\" \") is : " + spaceIdx + "\n");
  
// Use the substring method parse out the first name and print it.
firstName = custName.substring(0,spaceIdx);
System.out.println("First Name is : " + firstName);
}
}

Output

Exercise 2

NameMaker.java

/

//To import classes from the util package
import java.util.*;
public class NameMaker {

   public static void main(String args[]){
  
       String firstName, middleName, lastName;
      
       // Declare the object and initialize with
// predefined standard input object
       Scanner reader = new Scanner(System.in);
       System.out.println("Enter a firstName: ");
       // String input
       firstName = reader.nextLine();
       System.out.println("Enter a middleName: ");
       // String input
       middleName = reader.nextLine();
      
       System.out.println("Enter a middleName: ");
       // String input
       lastName = reader.nextLine();
       //Close your scanner
       reader.close();
      
       //Concat all three first mid and lastName as
       String fullName = firstName + " " + middleName + " " + lastName ;

       System.out.println("Full Name is : "+ fullName + )" using + operator");
      
       //Using Concat method
       String fullName2=(new StringBuilder()).append(firstName).append(" ").append(middleName).append(" ").append(lastName).toString();
       System.out.println("Full Name is :("+ fullName2 + ") using concat method");
   }  
}

Output

EXERCISE 2

As while using the concatenate + operator you can concatenate A string with any data type.

like you can concat numbers with strings

example: String numberOfBats = 10 + " Bats";

OUTPUT here will be: 10 Bats

But a concatenate method could only be able to concatenate strings (i.e you can't concat strings with numbers etc.)

As in this question, All three user inputs are Strings. you can easily use either concat using append or use + operator without using the concat method.

Here I will prefer the + operator method.

Add a comment
Know the answer?
Add Answer to:
Java Homework Problems: 4. • Examine AddImport.java. – Perform the following: – Replace the fully qualified...
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
  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • read the code and comments, and fix the program by INSERTING the missing code in Java...

    read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....

  • 8.26 Encapsulate the Name class. Modify the existing code shown below to make its fields private,...

    8.26 Encapsulate the Name class. Modify the existing code shown below to make its fields private, and add appropriate accessor methods to the class named getFirstName, getMiddleInitial, and getLastName. code: class Name { private String firstName; private String middleNames; private String lastName;    //Constructor method public Name(String firstName, String middleNames, String lastName) { this.firstName = firstName; this.middleNames = middleNames; this.lastName = lastName;    } //Accessor for firstName public String getFirstName() { return firstName; } //Accessor for middleNames public String getMiddlesNames()...

  • Write a program that reads a person's first and last names, separated by a space. Then...

    Write a program that reads a person's first and last names, separated by a space. Then the program outputs last name, comma, first name. End with newline. Example output if the input is: Maya Jones Jones, Maya import java.util.Scanner; public class SpaceReplace {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       String firstName;       String lastName; //answer goes here//    } }

  • JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File;...

    JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File; import java.io.IOException; import java.util.*; public class M1 {    public static void main(String[] args) {        //scanner input from user    Scanner scanner = new Scanner(System.in); // System.out.print("Please enter your full name: "); String fullname = scanner.nextLine(); //creating a for loop to call first and last name separately int i,k=0; String first="",last=""; for(i=0;i<fullname.length();i++) { char j=fullname.charAt(i); if(j==' ') { k=i; break; } first=first+j;...

  • In Java This is the method we did in class. How do I reverse the string...

    In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); }    public static String printBackwards(String one)...

  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • Intro to Java - Assignment JAVA ASSIGNMENT 13 - Object Methods During Event Handling Assignment 13...

    Intro to Java - Assignment JAVA ASSIGNMENT 13 - Object Methods During Event Handling Assignment 13 Assignment 13 Preparation This assignment will focus on the use of object methods during event handling. Assignment 13 Assignment 13 Submission Follow the directions below to submit Assignment 13: This assignment will be a modification of the Assignment 12 program (see EncryptionApplication11.java below). Replace the use of String concatenation operations in the methods with StringBuilder or StringBuffer objects and their methods. EncryptTextMethods.java ====================== package...

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